AL Codeunit to create Purchase Line

So I have an AL project that’s installed as a BC 365 v19.5 on-prem extension. The Codeunit procedure that creates a new Purchase Header works fine. I can see the new entry in the Purchase Orders list. But I have a separate Codeunit procedure that needs to add a new Purchase Line to that Purchase Header. Looking at the code I appear to be cross-referencing the linked field(s). Which is primarily the PurchLine.“Document No.” => PurchHeader.“No.”, correct?

Here is the 404 error response body I get when trying to add the new Purchase Line:

{“error”:{“code”:“Internal_InvalidTableRelation”,“message”:“The field Document No. of table Purchase Line contains a value (PO100029) that cannot be found in the related table (Purchase Header). CorrelationId: fb67003b-60bb-4bd6-a990-db6a58529a84.”}}

Any suggestions on how to resolve this? I can clearly see the Document No. in the Purchase Orders list. I am properly setting the PurchLine.“Line No.” value, the PurchLine.“Document Type” value, the PurchLine.“Type” value, etc. I’ll include a partial code snippet below.

        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.Validate("Document No.", PurchaseHdr."No.");
            PurchaseLn.Validate("Document Type", PurchaseHdr."Document Type");
            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("Type", PurchaseLnType::Item);
            ...
1 Like

Figured it out. I placed some test code prior to the Purchase Line being inserted and could determine that code block never even made it to the record insert. I changed the PurchaseLn.Validate(“Document No.”, PurchaseHdr.“No.”); to be a hard-coded PurchaseLn.“Document No.” := PurchaseHdr.“No.”; assignment and we are back in business. I am used to assigning values using Validate() but in this case it didn’t work as expected.

The one (hopefully last) hurdle I have is that the unit cost is not being assigned to the new Purchase Line. I thought that the field I needed to hit was the PurchaseLn.“Direct Unit Cost” field. Doing so the assigned value still isn’t showing up. I have defined the Unit of Measure Code, the Quantity, and the Quantity (Base) field values, and they appear in there. Anyone know what I could be missing?

Disregard that last hurdle. A custom third party extension was causing this issue. When I bypass it then the Purchase Line is inserted just fine. Direct Unit Cost included. :grinning:

How did you manage to bypass the third party extension? I’m having the same problem, “Direct Unit Cost” is assigned but value isn’t showing up.