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

A custom purchase order API issue is occurring while deep inserting in Business Central

(4) ShareShare
ReportReport
Posted on by 117
Dear experts,

I am creating Custom Purchase order API for deep insert into Business Central as I sent these APIs in Postman. Purchase header is created but the purchase line is not created. I have attached all API pages code and Postman tool response please reply.

Here's how to send an API request using Postman

 
I am getting this response in the Postman tool
 
 
 
page 54003 PurchaseOrderAPI
{
    APIGroup = 'UCgroup';
    APIPublisher = 'UC';
    APIVersion = 'v2.0';
    ApplicationArea = All;
    Caption = 'purchaseOrderHeader';
    DelayedInsert = true;
    EntityName = 'purchaseOrderHeader';
    EntitySetName = 'purchaseOrdersHeader';
    PageType = API;
    SourceTable = "Purchase Header";
    ODataKeyFields = SystemId;
    SourceTableView = where("Document Type" = filter(Order));
 
    layout
    {
        area(Content)
        {
            repeater(General)
            {
                field(id; Rec.SystemId)
                {
                    ApplicationArea = All;
                }
                field(no; Rec."No.")
                {
                    ApplicationArea = All;
                }
                field(buyFromVendorNo; Rec."Buy-from Vendor No.")
                {
                    Caption = 'Buy-from Vendor No.';
                }
                field(buyFromVendorName; Rec."Buy-from Vendor Name")
                {
                    Caption = 'Buy-from Vendor Name';
                }
                field(yourReference; Rec."Your Reference")
                {
                    Caption = 'Your Reference';
                }
           
           
 
                part(PurchasLinePart; PurchaseLinepart)
                {
                    Multiplicity = Many;
                    EntityName = 'PurchaseOrderLineDemo';
                    EntitySetName = 'PurchaseOrderLinesDemo';
                    SubPageLink = headerId = field(SystemId);
                }
            }
        }
    }
}
 

Here is the Purchase Line part code

 page 54002 PurchaseLinepart
{
    ApplicationArea = All;
    Caption = 'PurchaseLinepart';
    PageType = ListPart;
    SourceTable = "Purchase Line";
    PopulateAllFields = true;
    DelayedInsert = true;
    ODataKeyFields = SystemId;
    AutoSplitKey = true;
    SourceTableView = where("Document Type" = filter(Order));


 
    layout
    {
        area(Content)
        {
            repeater(General)
            {
                field(SystemId; Rec.SystemId)
                {
                    ApplicationArea = All;
                }
                field(id; Format(rec.SystemId, 0, 4).ToLower())
                 {
                    ApplicationArea=all;
                 }
                field("Document No."; Rec."Document No.")
                {
                    ApplicationArea = All;
                }
                               
                field("Document Type"; Rec."Document Type")
                {
                    ApplicationArea = All;
                }
 
                field(headerId; Rec.headerId)
                {
                    ApplicationArea = All;
                }
                field("Line No."; Rec."Line No.")
                {
                    ApplicationArea = All;
                }
                field("Buy-from Vendor No.";Rec."Buy-from Vendor No.")
                {
                    ApplicationArea = All;
                }
                   
                  field(Type;Rec.Type)
                {
                        ApplicationArea = All;
                }
                           
                field("No."; Rec."No.")
                {
                    ToolTip = 'Specifies the number of the involved entry or record, according to the specified number series.';
                }
           
                field("Variant Code"; Rec."Variant Code")
                {
                    ToolTip = 'Specifies the variant of the item on the line.';
                }
 
                field("Unit Cost";Rec."Unit Cost")
                {
                    ApplicationArea = All;
                }
               
                field(Quantity; Rec.Quantity)
                {
                    ToolTip = 'Specifies the quantity of the sales order line.';
                }
                field(Description;Rec.Description)
                {
                    ApplicationArea = All;
                }
               
            }
        }
    }
 
    var
         IsDeepInsert: Boolean;
 
    trigger OnInsertRecord(BelowxRec: Boolean): Boolean
    var
        MyHeader: Record "Purchase Header";
        MyLine: Record "Purchase Line";
    begin
        if IsDeepInsert then begin
            MyHeader.GetBySystemId(rec.headerId);
            Rec."Document No." := MyHeader."No.";
            //rec.headerId := MyHeader.SystemId;
            MyLine.SetRange("Document No.", Rec."Document No.");
            if MyLine.FindLast() then
                Rec."Line No." := MyLine."Line No." + 10000
            else
                Rec."Line No." := 10000;
        end;
    end;
 
    trigger OnNewRecord(BelowxRec: Boolean)
    var
        MyHeader: Record "Purchase Header";
    begin
        IsDeepInsert := IsNullGuid(rec.headerId);
        if not IsDeepInsert then begin
            MyHeader.GetBySystemId(rec.headerId);
            Rec."Document No." := MyHeader."No.";
           // MyHeader.get(rec."Document No.");
           // rec.headerId := MyHeader.SystemId;
        end;
    end;
}
   
 
 
 
 
tableextension 54003 PLineTableExt extends "Purchase Line"
{
    fields
    {
        field(54000; "headerId"; Guid)
        {
            TableRelation = "Purchase Header".SystemId;
 
        }
 
        modify(Description)
        {
            trigger OnAfterValidate()
            var
                myInt: Integer;
            begin
                GetHeader();
            end;
        }
 
    }
 
    keys
    {
        // Add changes to keys here
    }
 
    fieldgroups
    {
        // Add changes to field groups here
    }
 
    var
        PurchaseHeader: Record "Purchase Header";
 
    var
    trigger OnInsert()
    var
 
    begin
        GetHeader();
    end;




 
    local procedure GetHeader()
    begin
        if PurchaseHeader.SystemId <> "HeaderId" then
            PurchaseHeader.GetBySystemId("HeaderId");
    end;
}
I have the same question (0)
  • André Arnaud de Calavon Profile Picture
    303,128 Super User 2026 Season 1 on at
    Moved the question to the Business Central forum.
  • Verified answer
    Faisal Qureshi Profile Picture
    117 on at
    Hello Everyone,
     
    There was only one very minor issue I was able to resolve, the entity set name should be in lower case and changing it, I was able to successfully deep insert both purchase headers and purchase order line in business central.
     
     

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!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,032 Super User 2026 Season 1

#2
Dhiren Nagar Profile Picture

Dhiren Nagar 1,049 Super User 2026 Season 1

#3
YUN ZHU Profile Picture

YUN ZHU 1,029 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans