Skip to main content

Notifications

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 144
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.
  • Suggested answer
    Sami Ullah Profile Picture
    Sami Ullah 284 on at
    Custom API to Create Sales Order in on-prem BC

    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:

  • Ramiz Profile Picture
    Ramiz 344 on at
    Custom API to Create Sales Order in on-prem BC
    Hi,

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

     

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

News and Announcements

Announcing Category Subscriptions!

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,359 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,370 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans