microsoft-dynamicsdynamics-365dynamics-navdynamics-business-centralbusinesscentral

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


I'm trying to do multiple line inserts in a single call to a Business Central API

Right now, the test page I have is this one:

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 SalesOrderLines part page:

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?


Solution

  • Deep insert should be possible. Subpage must have an entity defined for this.

    For some reason in my code I have part name equal to the EntitySetName but this was written long tome ago so I'm not sure if it is required.

    page 60000 "Sales Order Inserts MIM API"
    {
        PageType = API;
        SourceTable = "Sales Order Inserts MIM";
        Caption = 'Sales Order Inserts MIM API';
    
        EntitySetName = 'SalesOrderInserts';
        EntityName = 'SalesOrderInsert';
        ODataKeyFields = BatchID;
    
        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(SalesLineInserts; 60001)
                    {
    
                        EntityName = 'SalesLineInsert';
                        EntitySetName = 'SalesLineInserts';
    
                        ApplicationArea = All;
                        SubPageLink = "Batch ID" = field("Batch ID");
                    }
                }
            }
        }
    }