web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

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

Custom API to Create Sales Order in on-prem BC

(1) ShareShare
ReportReport
Posted on by 163
Hi All,
 
Good Morning
 
I am using On-Prem Business central.

I want to create a custom API using AL language, which will accept the below payload/request and then will create a sales order with line items also.
But i tried to do it, not able to achieve this. I have gone through many videos but not get any satisfied answer.
 
Does anyone have any idea about this,  or if you have any code snip please share.
 
Payload/Request :  This will contains sales order header detail along with line items.
{
    "orderDate": "2024-10-10",
    "postingDate": "2024-10-10",
    "customerId": "d46301ed-970b-ee11-962f-00224859f0ad",
    "currencyCode": "SGD",
    "salesperson": "LEROY",
    "salesOrderLines": [
        {
            "itemId": "eb230e11-160c-ee11-962f-00224859f0ad",
            "lineType": "Item",
            "lineObjectNumber": "10810900019",
            "description": "AXE BODY WASH (INDIA) 250ML X 12 AFRICA",
            "unitOfMeasureId": "add2b303-200c-ee11-962f-00224859f0ad",
            "unitOfMeasureCode": "CTN",
            "quantity": 1,
            "unitPrice": 17,
            "locationId": "4af3149a-6a0c-ee11-962f-00224859f0ad"
        }
    ]
}

Thanks for your Help.
I have the same question (0)
  • Ramiz Profile Picture
    637 on at
    Hi,

    If you can convert your payload to a base64 and pass it to a codeunit API function, you can achieve it.

     
  • Suggested answer
    Sami Ullah Profile Picture
    315 Moderator on at

    Hi,

    You can achieve this by writing a codeunit.

    Codeunit Example:

    codeunit 50200 "Create Sales Order API"
    {
        Permissions = tabledata "Sales Header" = rimd, tabledata "Sales line" = rimd;
    
        [ServiceEnabled]
        procedure CreateSalesOrderFromPayload(payload: Text): Text;
        var
            JsonObject: JsonObject;
            SalesHeader: Record "Sales Header";
            SalesLine: Record "Sales Line";
            SalesOrderLines: JsonArray;
            SalesOrderLine: JsonToken;
            JsonToken: JsonToken;
        begin
    
            JsonObject.ReadFrom(payload);
            SalesHeader.Init();
            if JsonObject.Get('orderDate', JsonToken) then
                SalesHeader."Order Date" := JsonToken.AsValue().AsDate();
    
            //Do same for all fields.
            SalesHeader.Insert();
            if JsonObject.Get('salesOrderLines', JsonToken) then begin
                SalesOrderLines := JsonToken.AsArray();
                foreach SalesOrderLine in SalesOrderLines do begin
                    SalesLine.Init();
                    SalesLine."Document Type" := SalesLine."Document Type"::Order;
                    SalesLine."Document No." := SalesHeader."No.";
                    if SalesOrderLine.AsObject().Get('description', JsonToken) then
                        SalesLine.Description := JsonToken.AsValue().AsText();
                    //Do same for all fields.
                    SalesLine.Insert();
                end;
            end;
        end;
    }
    

    Postman Example:

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,028 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,366 Super User 2026 Season 1

#3
Grigorios Mavrogeorgis Profile Picture

Grigorios Mavrogeorgis 1,175 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans