How to do deep inserts into a custom API in Business Central?

kuhikuhi Member Posts: 18
Based on this question: https://forum.mibuso.com/discussion/78013/how-to-insert-multiple-rows-in-a-business-central-api-with-a-single-call

I was testing and found the deep inserts. So I've decided to build a sample API page with a page part:
page 60000 "Sales Order Inserts MIM API"
{
    PageType = API;
    SourceTable = "Sales Order Inserts MIM";
    Caption = 'Sales Order Inserts MIM API';
    EntitySetName = 'SalesOrderInserts';
    EntityName = 'SalesOrderInsert';
    APIPublisher = 'Kuhicop';
    APIGroup = 'Kuhicop';
    DelayedInsert = true; 
    APIVersion = 'v1.0';   

    layout
    {
        area(content)
        {
            repeater(Group)
            {
                field("BatchID"; Rec."Batch ID")
                {
                    ApplicationArea = All;
                }
                field("BatchDate"; Rec."Batch Date")
                {
                    ApplicationArea = All;
                }

                part(SalesOrderLines; 60001)
                {
                    ApplicationArea = All;
                    SubPageLink = "Batch ID" = field("Batch ID");
                }
            }
        }
    }
}

This is the page part:
page 60001 "Sales Orders MIM"
{
    ApplicationArea = All;
    Caption = 'Sales Orders MIM';
    SourceTable = "Sales Orders MIM";
    APIGroup = 'Kuhicop';
    APIPublisher = 'Kuhicop';
    APIVersion = 'v1.0';
    DelayedInsert = true;
    EntityName = 'SalesOrder';
    EntitySetName = 'SalesOrders';
    PageType = API;
    
    layout
    {
        area(content)
        {
            repeater(General)
            {
                field("BatchID"; Rec."Batch ID")
                {
                    ToolTip = 'Batch ID';
                }
                field("DocumentNo"; Rec."Document No.")
                {
                    ToolTip = 'Specifies the value of the Document No. field.';
                }
                field("LineNo"; Rec."Line No.")
                {
                    ToolTip = 'Specifies the value of the Line No. field.';
                }
            }
        }
    }
}

I'm trying to POST this JSON with just a single header + line:
{
    "BatchID": "BATCH003",
    "BatchDate": "2024-05-09",
    "salesOrders": [
        {
            "DocumentNo": "PED002",
            "LineNo": 10000,
            "BatchID": "BATCH003"
        }
    ]
}

This is the response I'm getting:
{
    "@odata.context": "https://api.businesscentral.dynamics.com/v2.0/{{TENANT_ID}}/{{COMPANY}}/api/Kuhicop/Kuhicop/v1.0/$metadata#companies(c6452bcb-c1b3-eb11-9b52-002248818d7e)/SalesOrderInserts/$entity",
    "@odata.etag": "W/\"JzE5OzgwMDc0MzY2NzQ0NjcxOTQxOTExOzAwOyc=\"",
    "BatchID": "BATCH003",
    "BatchDate": "2024-05-09",
    "salesOrders": []
}

So, the header is created, but the lines not...

How to deep inserts in Business Central with a custom API?

Answers

  • TallyHoTallyHo Member Posts: 416
    The solution is in the url of the api call. You need a reference for the subpage entity name in it. Only then it will process the nested part of the json.
Sign In or Register to comment.