web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
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 787

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?
I have the same question (0)
  • Suggested answer
    Inge M. Bruvik Profile Picture
    1,165 Moderator on at

    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.

  • Greg Kujawa Profile Picture
    787 on at

    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);
                ...

  • Greg Kujawa Profile Picture
    787 on at

    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);

  • Suggested answer
    Inge M. Bruvik Profile Picture
    1,165 Moderator on at

                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
    787 on at

    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,165 Moderator on at

    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
    787 on at

    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!

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 1,922 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,078 Super User 2026 Season 1

#3
Khushbu Rajvi. Profile Picture

Khushbu Rajvi. 628 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans