You may want to use the Create Reserv. Entry codeunit, and the two functions CreateReservEntry.CreateReservEntryFor & CreateReservEntry.CreateEntry.
Basically, by calling those two functions, you can properly create an item tracking line for an document. The following example,is automatically creating an item tracking line for the full quantity of the purchase line. It might not match exactly what you need, but if you allowed the user to enter or import many item tracking details per document line, you would just loop this code changing the quantity, lot & serial. It then creates the reservation entry properly.
CreateReservEntry.CreateReservEntryFor(DATABASE::“Purchase Line”,
SubType,
“Document No.”,’’,
0,“Line No.”,“Qty. per Unit of Measure”,
“Quantity (Base)” - TrackingSpec.“Quantity (Base)”,
‘’,{this is the serial number parameter}
LotNo); //this field we figured out based on setup
CreateReservEntry.CreateEntry(“No.”,“Variant Code”,“Location Code”,’’,
PurchHeader.“Expected Receipt Date”,0D,0,
ResStatus);
The problem with the item tracking form is that it is based on temporary tables, which group item ledgers together, and it makes it very hard to see what is going on until the form is closed, then the reservation engine loops & groups and creates item tracking records. The other issue with this form is that you have to close it to go to the next line - if your customers use a lot of item tracking, this process becomes unusable, especially during inventory selection. Imagine a 20 line sales order, with 5 different lots being selected per sales line. You go to the line, open item tracking, open select entries, select a few lines, close that form, close item tracking, and repeat TWENTY times. We ended up building some different screens to avoid the item tracking windows, but we still used a great deal of their code, it was simply too complex to start from scratch.