From the current voucher object, while setting Inventory Entries or Ledger Entries automatically from within a User Defined Function, a blank record gets inserted at the beginning.
When a voucher is being entered, automatically the first object of the sub-objects are already available even before we start entering any values. In such a scenario, when we try to insert a new object, the second object will be created leaving aside the first object. Hence, all the subsequent objects are filled with an empty row at the beginning of the object.
In such a scenario, verifying whether the first object is already available is important. Hence, within a User Defined Function, verify the following before setting values.
Label1 : if $$NumItems:InventoryEntries > 0 AND $$LoopIndex = 1
Label2 : SET TARGET: InventoryEntries[1] ;; if the first object already exists
Label3 : ELSE:
Label4 : INSERT COLLECTION OBJECT : Inventory Entries
Label5 : ENDIF
This ensures that in all modes i.e., create, alter, and so on. it works smoothly.