web-servicesodatadynamics-365dynamics-business-central

Dynamics 365 API v2: Adding sales lines to an existing sales order


I am trying to add sales lines to an existing sales order using the standard API included with dynamics 365 business central. However I cannot find the correct syntax on the api request to accomplish this.

I have tried posting to the companies({id})/salesOrderLines endpoint, and the companies({id})/salesOrder({id})/salesOrderLines endpoint but no matter how I try to work my request I always get the "Invalid Request Body" error.

Here is an example of the request I sent to the companies({id})/salesOrderLines endpoint:

Here is my request body:

[
  {
    "id": "e92c39cb-f552-4d4f-b680-ad7ded2949d0",
    "documentId": "e80573b0-9c8b-ed11-bfba-001dd8b71ee3",
    "lineType": "Item",
    "lineObjectNumber": "H10-110013",
    "quantity": 1
  },
  {
    "id": "e92c39cb-f552-4d4f-b680-ad7ded2949d0",
    "documentId": "e80573b0-9c8b-ed11-bfba-001dd8b71ee3",
    "lineType": "Item",
    "lineObjectNumber": "H10-112117",
    "quantity": 1
  }
]

Here is the response I get back:

"{"error":{"code":"BadRequest","message":"Invalid Request Body CorrelationId: 241e540a-5af5-4516-83f2-fbc035f80389."}}"

I am able to post a sales order and its lines simultaneously using deep requests but with larger orders they hit the request limit and I need a way of splitting up the lines.


Solution

  • salesOrderLines endpoint expects an object and does not accept collections, so best you can do here is post each line in a separate request. Request body would look like this:

    {
        "lineType": "Item",
        "lineObjectNumber": "H10-110013",
        "quantity": 1
    }
    

    id and documentId can be added to the request, but not really required.

    Another option is to send the request on the $batch endpoint as described in the docs here: https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/webservices/use-odata-batch

    POST http://bc21-dev:7048/bc/api/v2.0/$batch

    {
        "requests": [
            {
                "method": "POST",
                "url": "http://bc21-dev:7048/bc/api/v2.0/salesOrders(dd3585b3-dd6c-ed11-81b4-6045bd8e5172)/salesOrderLines",
                "headers": {
                    "Company": "CRONUS International Ltd.",
                    "Content-Type": "application/json"
                },
                "body": {
                    "lineType": "Item",
                    "lineObjectNumber": "1920-S",
                    "quantity": 2,
                    "unitPrice": 420.4
                }
            },
            {
                "method": "POST",
                "url": "http://bc21-dev:7048/bc/api/v2.0/salesOrders(dd3585b3-dd6c-ed11-81b4-6045bd8e5172)/salesOrderLines",
                "headers": {
                    "Company": "CRONUS International Ltd.",
                    "Content-Type": "application/json"
                },
                "body": {
                    "lineType": "Item",
                    "lineObjectNumber": "1952-W",
                    "quantity": 1,
                    "unitPrice": 183.12
                }
            }
        ]
    }