web
You’re offline. This is a read only version of the page.
close
Skip to main content
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
    300,257 Super User 2025 Season 2 on at
    A custom purchase order API issue is occurring while deep inserting in Business Central
    Moved the question to the Business Central forum.
  • Verified answer
    Faisal Qureshi Profile Picture
    117 on at
    A custom purchase order API issue is occurring while deep inserting in Business Central
    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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,915

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 2,011 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,989 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans