Search code examples
microsoft-dynamicsdynamics-navdynamics-business-centralbusinesscentral

How to insert multiple rows in a Business Central API?


I'm trying to insert a json with multiple records into Business Central via Web Services

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;
                }
            }
        }
    }
}

If I make a GET request, I see this response:

{
    "@odata.context": "https://api.businesscentral.dynamics.com/v2.0/{{TENANT_ID}}/{{ENVIRONMENT}}/api/Kuhicop/Kuhicop/v1.0/$metadata#companies({{COMPANY_ID}})/SalesOrderInserts",
    "value": [
        {
            "@odata.etag": "W/\"JzIwOzE1ODc4NzU5MDIzMDQyNDQyNTI1MTswMDsn\"",
            "BatchID": "BATCH001"
        },
        {
            "@odata.etag": "W/\"JzE5OzI3MTk3MjU4MTE5MDAwNDI1NTAxOzAwOyc=\"",
            "BatchID": "BATCH002"
        },
        {
            "@odata.etag": "W/\"JzE5OzgwMDc0MzY2NzQ0NjcxOTQxOTExOzAwOyc=\"",
            "BatchID": "BATCH003"
        }
    ]
}

Trying to insert from Postman against this URL: https://api.businesscentral.dynamics.com/v2.0/{{TENANT_ID}}/{{ENVIRONMENT}}/api/Kuhicop/Kuhicop/v1.0/companies({{COMPANY_ID}})/SalesOrderInserts

And based in the GET response, I will send a json With this data:

{
    "value": [
        {
            "BatchID": "BATCH004"
        },
        {
            "BatchID": "BATCH005"
        }        
    ]
}

But I get this error:

{
    "error": {
        "code": "BadRequest",
        "message": "The property 'value' does not exist on type 'Microsoft.NAV.salesOrderInsert'. Make sure to only use property names that are defined by the type.  CorrelationId:  a10912cb-7f47-4f38-b0a0-43f5dd595e3a."
    }
}

How to insert multiple rows in a Business Central custom API?


Solution

  • That won't work like you think. To insert multiple lines in one go you need to use $batch. I tried it once, it works, it is really cumbersome. I decided that inserting line-by-line is just easier. So if you don't really need to have a bunch of lines inserted in a transaction, then don't use it.

    Example

    https://services.odata.org/V4/(S(uvf1y321yx031rnxmcbqmlxw))/TripPinServiceRW/$batch
    User-Agent: Fiddler
    Authorization: <authz token>
    Content-Type: application/json
    Accept: application/json
    Host: localhost:9000
    Content-Length: 1234
    
    {
        "requests": [
        {
            "id": "1",
            "method": "PATCH",
            "atomicityGroup": "06d8a02a-854a-4a21-8e5c-f737bbd2dea8",
            "url": "https://services.odata.org/V4/(S(uvf1y321yx031rnxmcbqmlxw))/TripPinServiceRW/Me",
            "headers": {
            "content-type": "application/json; odata.metadata=minimal; odata.streaming=true",
            "odata-version": "4.0"
            },
            "body": {"@odata.type":"#Microsoft.OData.SampleService.Models.TripPin.Person","[email protected]":"#Collection(Microsoft.OData.SampleService.Models.TripPin.Location)","AddressInfo":[{"@odata.type":"#Microsoft.OData.SampleService.Models.TripPin.Location","Address":"P.O. Box 555","City":{"@odata.type":"#Microsoft.OData.SampleService.Models.TripPin.City","CountryRegion":"United States","Name":"Lander","Region":"WY"}}],"Concurrency":635657333837618321,"[email protected]":"#Collection(String)","Emails":["[email protected]","[email protected]"],"FirstName":"April","[email protected]":"#Microsoft.OData.SampleService.Models.TripPin.PersonGender","Gender":"Female","LastName":"Test","UserName":"aprilcline"}
        },
        {
            "id": "2",
            "method": "PATCH",
            "atomicityGroup": "06d8a02a-854a-4a21-8e5c-f737bbd2dea8",
            "url": "https://services.odata.org/V4/(S(uvf1y321yx031rnxmcbqmlxw))/TripPinServiceRW/Me/Trips(1001)",
            "headers": {
            "content-type": "application/json; odata.metadata=minimal; odata.streaming=true",
            "odata-version": "4.0"
            },
            "body": {"@odata.type":"#Microsoft.OData.SampleService.Models.TripPin.Trip","Budget":3000,"Description":"Updated Trip","EndsAt":"2014-01-04T00:00:00Z","Name":"Trip in US","ShareId":"9d9b2fa0-efbf-490e-a5e3-bac8f7d47354","StartsAt":"2014-01-01T00:00:00Z","[email protected]":"#Collection(String)","Tags":["Trip in New York","business","sightseeing"],"TripId":1001}
        }
        ]
    }