ODATA - Adding header and lines in one call/round trip

Hi :)

Hopefully someone here can help me with this, I'm finding useful documentation and examples for ODATA in Business Central hard to come by.

I have custom tables ("Inbound Job" and "Inbound Job Task Line"), the usual page (InboundJobQuotes) and subform (serviceLines) setup for ODATA web services. Using Postman I can create a header as follows:
POST [..] InboundJobQuotes
{
    "jobno":"JO001",
}
..and a subform line as follows:
POST [..] InboundJobQuotesserviceLines
{
    "jobno": "JO001",
    "lineno": 1,
    "description":, "Description JO001"
}
But that's two round trips. Ideally (And surely it should support this) I want this:
POST [..] InboundJobQuotes
{
    "jobno":"JO001",
    InboundJobQuotesserviceLines: [
        {
            "lineno": 1,
            "description":, "Description JO001"
        }
    ]
}
I get no error, but no lines are created either.

Anyone know what I'm doing wrong?

TIA

Best Answer

Answers

  • JuhlJuhl Member Posts: 724
    Been there and gave up!

    I use a page with one field, a global text variable. And I post the json payload to this variable and parse the payload myself with json.net in OnInsert trigger of the page.
    Follow me on my blog juhl.blog
  • foo_barfoo_bar Member Posts: 91
    This works with the API endpoint, not the ODATA endpoint.
    This is called a 'deep insert'

    for example: https://forum.mibuso.com/discussion/72423/api-deep-insert-not-working-in-custom-entity
  • HeidiAlfordHeidiAlford Member Posts: 4
    I did wonder if this was down to my misunderstanding of how the API works. I've been using SOAP services in NAV for years but how the API is setup is confusing me.

    My main API page has an EntitySetName of 'jobQuotes', the subform has an EntitySetName of 'serviceLines':
    part(serviceLines; "Inb. Service Line Entity")
    {
        EntitySetName = 'serviceLines';
        EntityName = 'serviceLine';
        SubPageLink = "Job Id" = field(SystemId);
    }
    

    I added it to the Web Service table as per usual as InboundJobQuotes, I'm not sure if that's the right way to reference the API /shrug

    When I call it from Postman I use the usual endpoint (As specified in Web Services), when I do the deep insert it insists I call it InboundJobQuotesserviceLines rather than serviceLines.

    EG.
    POST [..] InboundJobQuotes
    {
        "jobno":"JO001",
        InboundJobQuotesserviceLines: [
            {
                "lineno": 1,
                "description":, "Description JO001"
            }
        ]
    }
    

    When I was expecting...
    POST [..] InboundJobQuotes
    {
        "jobno":"JO001",
        serviceLines: [
            {
                "lineno": 1,
                "description":, "Description JO001"
            }
        ]
    }
    

    Postman will complain about untyped values if I use serviceLine.

    I know I'm doing something wrong based upon my misunderstanding of how this works. I wish they'd let me use SOAP lol.

    Bonus question: Can we use batch requests with ODATA?

    Thanks

    H
  • foo_barfoo_bar Member Posts: 91
    Correction, batching should be supported as mentioned on https://docs.microsoft.com/en-us/dynamics-nav/api-reference/v1.0/dynamics-rate-limits
    (I have never tried it myself)
  • HeidiAlfordHeidiAlford Member Posts: 4
    @foo_bar Thanks :D As it turns out this has been a complete misunderstanding of how I access the API. Don't bother putting it in the web services table, most importantly use the right endpoint.

    It makes so much more sense now lol.

    Thanks again!
Sign In or Register to comment.