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

create Json file and add data from "LSC Trans. Sales Entry" table

(1) ShareShare
ReportReport
Posted on by 80
Please anyone help me how to fix the code to write Json file using AL code for Business Central.
 
I have code, Please help me with correction
local procedure TransmitInvoice(JsonPayload: Text; Token: Text)
    var
        Client: HttpClient;
        Request: HttpRequestMessage;
        Response: HttpResponseMessage;
        ContentHeaders: HttpHeaders;
        RequestHeaders: HttpHeaders;
    begin
        Request.SetRequestUri('https://mra-api-url.example.com/submit');
        Request.Method := 'POST';
        Request.Content.WriteFrom(JsonPayload);
        Request.Content.GetHeaders(ContentHeaders);
      //  ContentHeaders.set('Content-Type', 'application/json');
        ContentHeaders.Add('Content-Type', 'application/json');
        Request.GetHeaders(RequestHeaders);
        RequestHeaders.Add('Authorization', 'Bearer ' + Token);
 
        if not Client.Send(Request, Response) then
            Error('Failed to send request');
 
        if not Response.IsSuccessStatusCode() then
            Error('MRA submission failed with status: %1', Response.HttpStatusCode());
    end;
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    17,504 Super User 2025 Season 1 on at
    create Json file and add data from "LSC Trans. Sales Entry" table
    Use JsonObject and JsonArray to build the payload from "LSC Trans. Sales Entry", then send it using HttpClient with proper headers. Ensure Content-Type is set to application/json and add Authorization header with the token.
  • Suggested answer
    YUN ZHU Profile Picture
    85,676 Super User 2025 Season 1 on at
    create Json file and add data from "LSC Trans. Sales Entry" table
    Just to add another method, since BC26 you can write json directly in text, which is also convenient for simple structures.
     
    Thanks.
    ZHU
  • Gerardo Rentería García Profile Picture
    20,851 Most Valuable Professional on at
    create Json file and add data from "LSC Trans. Sales Entry" table
  • Suggested answer
    RockwithNav Profile Picture
    7,187 on at
    create Json file and add data from "LSC Trans. Sales Entry" table
    ContentHeaders.Remove('Content-Type');
    ContentHeaders.Add('Content-Type', 'application/json');
     
    Add this and then try executing.
     
  • KK-21020735-0 Profile Picture
    80 on at
    create Json file and add data from "LSC Trans. Sales Entry" table
    snippet of the JSON body
    -----------------------
    Thank you both of you for instance help.
     
    local procedure BuildPOSPayload(SalesEntry: Record "LSC Trans. Sales Entry"): Text
        var
            JsonRoot, Invoice, LineItem : JsonObject;
            LineArray: JsonArray;
            TempEntry: Record "LSC Trans. Sales Entry";
            tostring: Text;
        begin
            Invoice.Add('invoiceIdentifier', SalesEntry."Transaction No.");
            // Invoice.Add('cashierId', SalesEntry."Staff ID"); // Removed because 'Staff ID' does not exist in the record
     
            TempEntry.CopyFilters(SalesEntry);
            if TempEntry.FindSet() then begin
                repeat
                    Clear(LineItem);
                    LineItem.Add('itemNo', TempEntry."Item No.");
                    LineItem.Add('productCodeOwn', TempEntry."Item No.");
                    LineItem.Add('itemDesc', TempEntry."Item Description");
                    LineItem.Add('quantity', Format(TempEntry.Quantity));
                    LineItem.Add('unitPrice', Format(TempEntry.Price));
                    LineItem.Add('discount', Format(TempEntry."Discount Amount"));
                    LineItem.Add('amtWoVatCur', Format(TempEntry."Net Amount"));
                    LineItem.Add('vatRate', Format(TempEntry."VAT Amount"));
                    LineArray.Add(LineItem);
                until TempEntry.Next() = 0;
            end;
     
            Invoice.Add('lineItems', LineArray);
            JsonRoot.Add('invoice', Invoice);
            JsonRoot.WriteTo(tostring);
            exit(tostring);
        end;
  • Sohail Ahmed Profile Picture
    4,747 on at
    create Json file and add data from "LSC Trans. Sales Entry" table
    I just noticed you’ve attached an error file — that’s very helpful. If possible, please also attach a snippet of the JSON body you're sending. That will make it easier for us to review and help identify the root cause more quickly.
  • Suggested answer
    Sohail Ahmed Profile Picture
    4,747 on at
    create Json file and add data from "LSC Trans. Sales Entry" table

    Sorry, I know this isn't the most formal way to answer in the community — but to save time, I’m sharing a quick code snippet as a hint on how you can create a JSON body using AL.

     

    If you need help building a more complex JSON structure, feel free to let me know — I’ll be happy to guide you when I have availability.

     

     

     

    Mark below checkbox to make this answer Verified if it helps you

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

Ramesh Kumar – Community Spotlight

We are honored to recognize Ramesh Kumar as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

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

#1
Sohail Ahmed Profile Picture

Sohail Ahmed 2,655

#2
Mansi Soni Profile Picture

Mansi Soni 1,574

#3
YUN ZHU Profile Picture

YUN ZHU 1,453 Super User 2025 Season 1

Featured topics

Product updates

Dynamics 365 release plans