Skip to main content
Business Central forum
Answered

Error on sending attachment file to Sales Order via API

editSubscribe (0) ShareShare
ReportReport
Posted on by 16

Hi Dynamics community!

I am having an issue with Sales Order attachments via API (through Postman).

I can successfully create an attachment object for a given sales order like so:

POST api.businesscentral.dynamics.com/.../attachments

 with the body containing:

{

    "parentId" : "Sales Order ID",

    "fileName": "TEST.pdf",

    "parentType": "Sales Order"

}

 However, if I now try to add the attachment content like so (with IF-MATCH for the odata tag):

PATCH api.businesscentral.dynamics.com/.../attachments(parentId=salesOrderId,id=attachmentId)/attachmentContent

 I receive an error message:

 {

    "error": {

        "code": "BadRequest_NotFound",

        "message": "The number of keys specified in the URI does not match number of key properties for the resource 'Microsoft.NAV.attachment'."

    }

}

And in Business Central the Sales Order in question still shows no attachment.

 

Conversely, if I upload an attachment to the SO and try to retrieve it via the API, this SO seems to have no attachments at all.

Somewhere there is a disconnect between what I’m doing on BC, and what I’m doing via the API.

Any ideas or suggestions would be greatly appreciated!

 

Categories:
Attachments
  • Jakob Gutzmann Profile Picture
    Jakob Gutzmann 16 on at
    RE: Error on sending attachment file to Sales Order via API

    Good evening Marco,

    Thank you very very much for your help!

    I didn't even think about only using the attachment_id in the URL for attachmentContent.

    The example in the docs uses parent_id and attachment_id, and I couldn't mentally distance myself enough from that example to try just with the attachment id.

    Thank you very much again, this is bringing me a huge step forwards in my testing.

    Enjoy your weekend,

    Jakob

  • Verified answer
    Marco Mels Profile Picture
    Marco Mels on at
    RE: Error on sending attachment file to Sales Order via API

    Hello,

    Okay, I continued the testing.

    1. create the Purchase Invoice, as per docs, this goes to Incoming Documents (and not to Attachments); Using attachments in the url may be a bit confusing as it was for me as well, but it is table 130; note that creating the Purchase Invoice is not required of course, you can use an existing one

    2. create the attachment, bite size is 0 (empty place holder)

    3. fill the attachment with data via patch

    POST:

    https:    //api.businesscentral.dynamics.com/v2.0/{{TENANTAADID}}/{{PRODUCTION}}/api/v2.0/companies({{COMPANY}})/purchaseInvoices

    {

       "vendorNumber": "10000",

        "vendorInvoiceNumber": "POSTAPI 107209"

    }

    GET:

    https:      //api.businesscentral.dynamics.com/v2.0/{{TENANTAADID}}/{{PRODUCTION}}/api/v2.0/companies({{COMPANY}})/purchaseInvoices

    Copy ID that is relevant to the newly created invoice     "id": "d856f038-fa3a-ed11-946f-000d3a472e7a",

    POST:

    https:       //api.businesscentral.dynamics.com/v2.0/{{TENANTAADID}}/{{PRODUCTION}}/api/v2.0/companies({{COMPANY}})/attachments

    {

        "parentId""d856f038-fa3a-ed11-946f-000d3a472e7a",
        "fileName""athene tafel.png",
        "parentType""Purchase_x0020_Invoice"
    }

    Copy ID:     "id": "ca12d657-503b-ed11-946f-002248834669", there is no need to upload under Binary (in the earlier posting that was not needed). The bite size is indeed 0 (attachment is empty), 


    PATCH:
    https:       //api.businesscentral.dynamics.com/v2.0/{{TENANTAADID}}/{{PRODUCTION}}/api/v2.0/companies({{COMPANY}})/attachments(ca12d657-503b-ed11-946f-002248834669)/attachmentContent


    Now add the file via Binary, upload file, athene tafel.png

    The response will be 204 No content and the file is there and can be opened:

    pastedimage1663946252687v1.png

    pastedimage1663946337957v2.png

    Hope it is clear so you can proceed with your testing.

  • Jakob Gutzmann Profile Picture
    Jakob Gutzmann 16 on at
    RE: Error on sending attachment file to Sales Order via API

    Awesome, thank you very much Marco!

    I was using the update attachment, because I had understood the documentation to include these steps:

    First create the attachment object (like you described) and secondly add the content to the attachment object.

    But I may have this totally backwards.

  • Suggested answer
    Marco Mels Profile Picture
    Marco Mels on at
    RE: Error on sending attachment file to Sales Order via API

    Hello,

    No problem. I used the create attachment:
    Create attachment - Business Central | Microsoft Learn

    What you describe is update attachment:
    Upload attachment - Business Central | Microsoft Learn

    if I find the time to look at this today, then I will post my comments. Otherwise, i will look into this on Monday (working in EMEA time zone). 

    Thanks.

  • Jakob Gutzmann Profile Picture
    Jakob Gutzmann 16 on at
    RE: Error on sending attachment file to Sales Order via API

    Hi Marco,

    First of all, I really really appreciate that you take the time to dig into my problem. Thank you very much!

    Secondly, I recreated your suggestion and these are my findings:

    - Creating attachment object for Purchase Invoices works just as you suggested.

    - But these attachments do not show up under "Attachments", but rather "Details > Incoming Document Files"

    - The attachments themselves are still empty ("byteSize": 0)

    - Creating attachment object for Sales Orders also works

    - Sales Orders do not have "Incoming Document Files" under "Details", at least I can't find them.

    - Lastly, trying to put actual content into the attachment file with this url (.../attachments(parentId=parentID,id=ID)/attachmentContent) still leads to the same error.

    {

       "error": {

           "code": "BadRequest_NotFound",

           "message": "The number of keys specified in the URI does not match number of key properties for the resource 'Microsoft.NAV.attachment'."

       }

    }

    I feel that I am fundamentally misunderstanding what attachments, incoming documents, and attachmentContent really are in Business

    Central.    ¯\_(ツ)_/¯

  • Suggested answer
    Marco Mels Profile Picture
    Marco Mels on at
    RE: Error on sending attachment file to Sales Order via API

    Hello,

    Just to let you know I did try it myself in a SaaS environment. In Postman I added a new tab:

    POST https   :    //api.businesscentral.dynamics.com/v2.0/{{TENANTAADID}}/{{PRODUCTION}}/api/v2.0/companies({{COMPANY}})/purchaseInvoices

    In Body, Raw, the following was added:

    {

       "vendorNumber": "10000",

        "vendorInvoiceNumber": "POSTAPI 107209"

    }

    In Headers, the following was added Content-Type application/json.

    The result is that a new Purchase Invoice was created. Next we need to get the ID, so we copy it from the output:

       "id": "d856f038-fa3a-ed11-946f-000d3a472e7a",

    Next we add a new tab:

    POST:

    https   ://   api.businesscentral.dynamics.com/v2.0/{{TENANTAADID}}/{{PRODUCTION}}/api/v2.0/companies({{COMPANY}})/attachments

    In Body, Raw, the following was added:

    {

       "parentId": "d856f038-fa3a-ed11-946f-000d3a472e7a",

       "fileName": "athene tafel.png",

       "parentType": "Purchase_x0020_Invoice"

    }

    Under Binary, the "athene tafel.png" file was uploaded:

    Again, the Content-Type is set to application/json (yours is set to application/pdf). The parentId was taken from the copied value.

    The result is like this:

    pastedimage1663913597513v1.png

    NOTE: I reformatted the url's because it is otherwise not viewable as formatting changes

    Thanks.

  • Jakob Gutzmann Profile Picture
    Jakob Gutzmann 16 on at
    RE: Error on sending attachment file to Sales Order via API

    Hi Marco, thank you so much for answering! Unfortunately I am still getting the same error.

    Here are two screenshots of my postman header and body, maybe that will make troubleshooting easier.

    Screen-Shot-2022_2D00_09_2D00_22-at-13.24.25.png

    Screen-Shot-2022_2D00_09_2D00_22-at-13.26.50.png

  • Suggested answer
    Marco Mels Profile Picture
    Marco Mels on at
    RE: Error on sending attachment file to Sales Order via API

    Hi,

    Do not specify the etag, but use * instead.

    Thanks.

Helpful resources

Quick Links

New Blog Features Released!

Check out the new community blog features for viewers and authors…

Setting Up Knowledge Sources for Copilot…

Look at how configuring a comprehensive knowledge base is crucial…

Demystifying Copilot with Georg Glantschnig…

Industry experts answer burning questions directly from our amazing Community…

Leaderboard

#1
Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 283,126 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 222,646 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,138

Featured topics

Product updates

Dynamics 365 release plans