Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested answer

How to create an API query/page/codeunit with the following format

(0) ShareShare
ReportReport
Posted on by 5

Hi All,

I’m actually trying to export all the released sales/purchase orders headers and it’s lines to JSON using API. I want the below format where header repeats only once.
{
“salesheaderNO”: “{{No}}”,
“salesOrderLines”: [
{
“itemId”: “{{itemId}}”,
“quantity”: 5
},
{
“itemId”: “{{itemId}}”,
“quantity”: 3
},
{
“itemId”: “{{itemId}}”,
“quantity”: 8
}
]
}{
“customerId2”: “{{customerId2}}”,
“salesOrderLines”: [
{
“itemId2”: “{{itemId2}}”,
“quantity”: 5
},
{
“itemId2”: “{{itemId2}}”,
“quantity”: 3
},
{
“itemId2”: “{{itemId2}}”,
“quantity”: 8
}
]
}

I have tried it in Query like below :
query 50111 “RelPurchaseOrders”
{
Caption = ‘Purchase orders’;
QueryType = API;
APIPublisher = ‘Test’;
EntitySetName = ‘PurchaseOrder’;
EntityName = ‘Test’;
APIGroup = ‘TestPOOrders’;
APIVersion = ‘v1.0’;

elements
{

    dataitem(Purchase_Header; "Purchase Header")
    {
        column(Document_Type; "Document Type") { }
        column(No_; "No.") { }
        column(Buy_from_Vendor_Name; "Buy-from Vendor Name") { }
        column(Vendor_Order_No_; "Vendor Order No.") { }
        column(Location_Code; "Location Code") { }
        column(Due_Date; "Due Date") { }

        dataitem(Purchase_Line; "Purchase Line")
        {
            column(Document_No_; "Document No.") { }
            column(No_; "No.") { }
            column(Type; Type) { }
            column(Quantity; Quantity) { }
            column(Location_Code; "Location Code") { }
            column(Unit_of_Measure; "Unit of Measure") { }
            column(Unit_Cost; "Unit Cost") { }
        }
    }
}

}

but the format I’m getting is header and lines are getting combined and they are not sepeated as I wanted above
“value”: [
{
“Document_Type”: “Order”,
“No_”: “106002”,
“Buy_from_Vendor_Name”: "A R Enterprise ",
“Vendor_Order_No_”: “”,
“Location_Code”: “EMIRATES”,
“Due_Date”: “2023-03-13”

        "Document_No_": "106002",
        "No_": "1006",
        "Type": "Item",
        "Quantity": 500,
        "Location_Code": "EMIRATES",
        "Unit_of_Measure": "Kilo",
        "Unit_Cost": 27.67,
        "AuxiliaryIndex1": "Order",
        "AuxiliaryIndex2": 10000
 
        "Document_No_": "106002",
        "No_": "FREIGHT",
        "Type": "Charge (Item)",
        "Quantity": 1,
        "Location_Code": "EMIRATES",
        "Unit_of_Measure": "Kilo",
        "Unit_Cost": 540,
        "AuxiliaryIndex1": "Order",
        "AuxiliaryIndex2": 20000
  
    },

If you see header is once but both the lines are combined I’ve just entered space to show you clearly. Let me know how can I achieve the required format?

  • Suggested answer
    knsaicharan1 Profile Picture
    5 on at
    RE: How to create an API query/page/codeunit with the following format

    www.dynamicsuser.net/.../78039

  • knsaicharan1 Profile Picture
    5 on at
    RE: How to create an API query/page/codeunit with the following format

    Yeah I have tried the output is coming it's just the format I neded

    DataItemLink = "Document Type" = Purchase_Header."Document Type", "Document No." = Purchase_Header."No.";

  • Suggested answer
    Deekshitha Reddy Profile Picture
    219 on at
    RE: How to create an API query/page/codeunit with the following format

    Did you try using property "DataItemlink".To link the tables.

    learn.microsoft.com/.../devenv-query-links-joins

    Hope this helps.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,884 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,758 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans