Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

Creating a new Purchase Line in AL

(0) ShareShare
ReportReport
Posted on by 385

My AL project is deployed in an on-prem BC 365 v19.5 test environment. One task I need to perform is import in purchase orders and their lines using AL code. So far I can create a new Purchase Header, and then create a new Purchase Line associated with the Purchase Header. The Purchase Line fields appear to be populating correctly. Except for the Direct Unit Cost Excl. Tax field, as it appears in the subform in the web UI. I thought that this code would accomplish that:

PurchaseLn.Validate("Direct Unit Cost", UnitCost);
This didn't do the trick. I've also manually assigned my unit cost to the other cost-related fields available in the Purchase Line. Still no luck. I have assigned the Unit of Measure Code, and can see it listed in the Purchase Line as it appears in the web UI. 
Anyone know of which specific field or method I need to reference in order to get the unit costs ported in?
  • Greg Kujawa Profile Picture
    385 on at
    RE: Creating a new Purchase Line in AL

    Yep, good looking out. When I bypassed that third party extension's fields then things worked fine. No wonder I was banging my head against the wall. Thanks for the extra set of eyes!

  • Suggested answer
    Inge M. Bruvik Profile Picture
    1,029 Moderator on at
    RE: Creating a new Purchase Line in AL

    Yeah i am pretty sure the code i gave you will work in a standard system. So if it is not working it is a good idea to investigate the other customizations.

  • Greg Kujawa Profile Picture
    385 on at
    RE: Creating a new Purchase Line in AL

    Unfortunately the same outcome. Before I insert the record there is a custom third party AL extension to Purchase Line table. I assign a few values there and then insert the new Purchase Line. Will try to comment that section out to see if that affects things, in case there are some validation triggers taking place.

  • Suggested answer
    Inge M. Bruvik Profile Picture
    1,029 Moderator on at
    RE: Creating a new Purchase Line in AL

                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.Validate("No.",ItemNo);
                PurchaseLn.Description := Descr;
                PurchaseLn."Unit of Measure Code" := 'PCS';
                PurchaseLn.Validate(Quantity,Qty);
                PurchaseLn.Validate("Direct Unit Cost",UnitCost);
                PurchaseLn.Validate("Location Code", LocnCode);
                PurchaseLn.Insert(true);


    Please try this code.

  • Greg Kujawa Profile Picture
    385 on at
    RE: Creating a new Purchase Line in AL

    For now (since that overkill exercise did not work), I simplified things back. Snippet below.

                PurchaseLn."Unit of Measure Code" := 'PCS';
                PurchaseLn."Quantity (Base)" := Qty;
                PurchaseLn.Validate(Quantity, Qty);
                PurchaseLn.Validate("Direct Unit Cost", UnitCost);

  • Greg Kujawa Profile Picture
    385 on at
    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);
                ...

  • Suggested answer
    Inge M. Bruvik Profile Picture
    1,029 Moderator on at
    RE: Creating a new Purchase Line in AL

    I do not know in what order you validate your fields.

    But it might be a good idea to look at the order. Try to validate the qty. before you validate the Direct Unit cost.

    Or maybe you can share your code, so we can have a look at it.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
Sohail Ahmed Profile Picture

Sohail Ahmed 1,452

#2
YUN ZHU Profile Picture

YUN ZHU 1,313 Super User 2025 Season 1

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 1,083 Most Valuable Professional

Featured topics

Product updates

Dynamics 365 release plans