Search code examples
odatadynamics-business-central

How do I update status of sales invoice via Business Central API


I am trying to update the status of the Sales Invoice in Microsoft Business Central via an update sales invoice API, but getting an error as mentioned below.

Here is the request's cURL:

curl --location --request PATCH 
https://api.businesscentral.dynamics.com/v2.0/<tenant id>/<environment name>/api/v2.0/companies(<company id>)/salesInvoices(<sales invoice id>)
-- header 'Authorization: Bearer <token>
-- header 'If-Match: <valid etag>'
-- header 'Content-Type: application/json'
-- data-raw '{
    "status": "Paid"
}'

Below is the response for above request:

  • Status: 400 Bad Request

  • Response body:

    <error xmlns="https://docs.oasis-open.org/odata/ns/metadata">
        <code>RequestDataInvalid</code>
        <message>Request data is invalid.</message>
    </error>
    

I have made sure that the tenant id, environment name, company id, sales invoice id, and the eTag are correct for the invoice whose status I need to update.

Also, I'm not sure why the above error response received is in application/xml type.

Can you please shed a light as what is going wrong here?

EDIT 1

I figured out what was the root cause of the above issue, I needed to remove the backslash from the ETag which I was getting from the GET API, and need to pass the same to the PATCH API.

But now, I am getting below error message while trying to update the status:

Control 'status' is read-only. CorrelationId: <id>

So, how can we update the status of a sales invoice in MS Business Central via an API? Or how can we mark the sales invoice as Paid in MS Business Central via an API?


Solution

  • I finally figured out how we can update the status (mark as Paid) of Sales Invoice in MS Business Central using the API.

    While there is no direct API/way to update the status, but we can do that by creating and posting the Cash Receipt Journal entry. But, here again there is no direct API available to POST the created Cash Receipt Journal entry, so I created a custom Extension (Codeunit) and installed it in my MS Business Central account, and triggered it via OData API.

    Here is the documentation about the API which we can use to create Cash receipt journal entry (Customer payment): https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/api-reference/v2.0/api/dynamics_customerpayment_create

    Here is the link to the code of the Codeunit (which will POST the journal entry) with the instructions on how to install it in MS Business Central, and how to trigger it via OData API: https://github.com/jigneshkhatri/MBC-Gen-Journal-Line-Posting-CodeUnit

    Hope this will help someone.