web
You’re offline. This is a read only version of the page.
close
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 101
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;
I have the same question (0)
  • Suggested answer
    Sohail Ahmed Profile Picture
    11,150 Super User 2025 Season 2 on at

    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

  • Sohail Ahmed Profile Picture
    11,150 Super User 2025 Season 2 on at
    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.
  • KK-21020735-0 Profile Picture
    101 on at
    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;
  • Suggested answer
    RockwithNav Profile Picture
    8,637 Super User 2025 Season 2 on at
    ContentHeaders.Remove('Content-Type');
    ContentHeaders.Add('Content-Type', 'application/json');
     
    Add this and then try executing.
     
  • Gerardo Rentería García Profile Picture
    25,236 Most Valuable Professional on at
  • Suggested answer
    YUN ZHU Profile Picture
    95,729 Super User 2025 Season 2 on at
    Just to add another method, since BC26 you can write json directly in text, which is also convenient for simple structures.
     
    Thanks.
    ZHU
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    20,601 Super User 2025 Season 2 on at
    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.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,238

#2
YUN ZHU Profile Picture

YUN ZHU 773 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 630

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans