RE: Creating a new Purchase Line in AL
Thanks for the reply! Actually I do assign the quantity field value prior to assigning the unit cost value. Funny you mentioned the Validate() logic. At first the line item wasn't being created at all if I validated the PurchaseLine."Document No." against the PurchaseHeader."No." value. The code would error out, complaining that it couldn't find the Purchase Header. When it was there all along. Finally I just fixed that by manually assigning the PurchaseLine."Document No." reference.
Anyhoo, here is a code snippet below. I know I'm essentially overkilling it my assigning every calculated cost and amount field known to man. :) Previously was just trying the one-liner in my original post.
PurchaseHdr.SetRange("No.", PoNum);
if PurchaseHdr.FindFirst() then begin
PurchaseLnLu.SetRange("Document No.", PoNum);
if PurchaseLnLu.FindLast() then
LineNo := PurchaseLnLu."Line No." 10000
else
LineNo := 10000;
PurchaseLn.Init();
PurchaseLn.Validate("Line No.", LineNo);
PurchaseLn."Document No." := PurchaseHdr."No.";
PurchaseLn.Validate("Document Type", PurchaseHdr."Document Type");
PurchaseLn.Validate("Type", PurchaseLnType::Item);
PurchaseLn.Validate("Buy-from Vendor No.", PurchaseHdr."Buy-from Vendor No.");
PurchaseLn.Validate("Pay-to Vendor No.", PurchaseHdr."Pay-to Vendor No.");
PurchaseLn.Validate("Currency Code", PurchaseHdr."Currency Code");
PurchaseLn."No." := ItemNo;
PurchaseLn.Description := Descr;
PurchaseLn."Unit of Measure Code" := 'PCS';
PurchaseLn."Quantity (Base)" := Qty;
PurchaseLn.Quantity := Qty;
PurchaseLn."Line Discount %" := 0.0;
PurchaseLn."Line Discount Amount" := 0.0;
PurchaseLn."Direct Unit Cost" := UnitCost;
PurchaseLn."Unit Cost" := UnitCost;
PurchaseLn."Unit Cost (LCY)" := UnitCost;
PurchaseLn.Amount := Qty * UnitCost;
PurchaseLn."Amount Including VAT" := Qty * UnitCost;
PurchaseLn."VAT Base Amount" := Qty * UnitCost;
PurchaseLn."Line Amount" := Qty * UnitCost;
PurchaseLn.Validate("Location Code", LocnCode);
...