Skip to main content

Notifications

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

Custom API to Create Sales Order in on-prem BC

Posted on by 97
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 281 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

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,900 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,297 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans