Create JSON file in AL for D365 BC:
(1) Introduction: A JSON file is a file that stores data in a structured way. It stands for JavaScript Object Notation. It is mainly used for transferring data from web applications to servers.
(2) Example of JSON File Structure as shown below.
{→Object “acesstoken”: [→Array { “username”:”abc@gmail.com”,→commas “password”: “abc@123”, “client_id”:”TlfvApUIVDUrm1234″, “client_secret”:”pwSpO2345aONhGHOlaHHPkWp”, “grant_type”:”password”→Values } ] } |
(3) JSON structure is a combination of:
- Data is in value pairs: ‘qwer’
- Data is separated by commas: ‘,’
- Curly braces hold objects: ‘{}’
- Square brackets hold arrays: ‘[]’
(4) In this blog we will see the steps of creating a JSON file in AL for D365 BC. If you develop a JSON file in the previous version then you will find major differences while developing a JSON file in AL.
(5) Let’s create Sales Invoice detail in JSON file format. For creating the JSON file, I am creating a new Function in the Codeunit object.
(6) For creating JSON Files in D365 BC, Microsoft introduces datatypes (JsonObject and JsonArray).
(7) In the previous version these datatypes were not available and the method of creating JSON files was also different.
(8) Follow the below images to create JSON file format.
(9) Steps for getting information of Sales Invoice in JSON format, as shown.
(10) According to the above images, I have declared JsonObjects like “JObjectDocumentDetails”, “JObjectSellerDetails”, “ JObjectBuyerDetails” and “JobjectItemList” and then combined these JsonObject in one JsonObject. If your JSON file format is not grouped then you declare only one JSON object. For more clarity refer to the below example of JSON files.
- Plain JSON:
{ “username”:”abc@gmail.com”, “password”: “abc@123”, “client_id”:”TlfvApUIVDUrm1234″, “client_secret”:”pwSpO2345aONhGHOlaHHPkWp”, “grant_type”:”password” } |
- Grouped JSON:
{ “acesstoken”: [ { “username”:”abc@gmail.com”, “password”: “abc@123”, “client_id”:”TlfvApUIVDUrm1234″, “client_secret”:”pwSpO2345aONhGHOlaHHPkWp”, “grant_type”:”password” } ] } |
Source Code:
codeunit 50001 “Custom Functions-01” { procedure CreateJSONSalesInvoice(SalesInvoiceHeader: Record “Sales Invoice Header”) var GetLocation: Record Location; GetCustomer: Record Customer; JObjectDocumentDetails: JsonObject; JObjectSellerDetails: JsonObject; JObjectBuyerDetails: JsonObject; JobjectItemList: JsonObject; SalesInvoiceLine3: Record “Sales Invoice Line”; ShiptoAddress: Record “Ship-to Address”; CustShipGstin: Code[15]; RefDocNo: Code[20]; SalesInvoiceHeader2: Record “Sales Invoice Header”; SalesInvoiceLine2: Record “Sales Invoice Line”; InvRoundingGL: Code[20]; SrNo: Integer; SalesInvoiceLine: Record “Sales Invoice Line”; DocumentNo: Code[20]; JObject: JsonObject; JsonText: Text; JArray: JsonArray; J: J CompanyInformation: record “Company Information”; begin DocumentNo := SalesInvoiceHeader.”No.”; CompanyInformation.get; GetLocation.GET(SalesInvoiceHeader.”Location Code”); GetLocation.TestField(“GST Registration No.”); GetCustomer.GET(SalesInvoiceHeader.”Bill-to Customer No.”); //Json Sales Invoice document detail JObjectDocumentDetails.Add(‘document_number’, SalesInvoiceHeader.”No.”); JObjectDocumentDetails.Add(‘document_date’, SalesInvoiceHeader.”Posting Date”); JObject.Add(‘document_details’, JObjectDocumentDetails); //Json Sales Invoice document detail //Json Sales Invoice Seller detail JObjectSellerDetails.Add(‘legal_name’, CompanyInformation.Name); JObjectSellerDetails.Add(‘trade_name’, GetLocation.Name); JObjectSellerDetails.Add(‘address1’, GetLocation.Address); JObjectSellerDetails.Add(‘address2’, GetLocation.”Address 2″); JObjectSellerDetails.Add(‘location’, GetLocation.City); JObjectSellerDetails.Add(‘pincode’, GetLocation.”Post Code”); JObject.Add(‘seller_details’, JObjectSellerDetails); //Json Sales Invoice Seller detail //Json Sales Invoice Buyer detail JObjectBuyerDetails.Add(‘legal_name’, SalesInvoiceHeader.”Bill-to Name”); JObjectBuyerDetails.Add(‘trade_name’, SalesInvoiceHeader.”Bill-to Name”); JObjectBuyerDetails.Add(‘address1’, SalesInvoiceHeader.”Bill-to Address”); JObjectBuyerDetails.Add(‘address2’, SalesInvoiceHeader.”Bill-to Address 2″); JObjectBuyerDetails.Add(‘location’, SalesInvoiceHeader.”Bill-to City”); JObject.Add(‘buyer_details’, JObjectBuyerDetails); //Json Sales Invoice Buyer detail //Json Sales Invoice Item List CLEAR(SrNo); SalesInvoiceLine.SETRANGE(“Document No.”, SalesInvoiceHeader.”No.”); SalesInvoiceLine.SETFILTER(“No.”, ‘<>%1’, InvRoundingGL); SalesInvoiceLine.SETFILTER(Quantity, ‘<>%1’, 0); IF SalesInvoiceLine.FINDSET THEN BEGIN REPEAT SrNo += 1; JobjectItemList.Add(‘item_serial_number’, SrNo); JobjectItemList.Add(‘product_description’, SalesInvoiceLine.Description); JobjectItemList.Add(‘quantity’, SalesInvoiceLine.Quantity); JobjectItemList.Add(‘unit’, SalesInvoiceLine.”Unit of Measure Code”); JobjectItemList.Add(‘unit_price’, ROUND(SalesInvoiceLine.”Unit Price” * 1, 0.01, ‘=’)); JobjectItemList.Add(‘total_amount’, ROUND((SalesInvoiceLine.”Unit Price” * SalesInvoiceLine.Quantity) * 1, 0.01, ‘=’)); until SalesInvoiceLine.Next = 0; JArray.Add(JobjectItemList); JObject.Add(‘item_list’, JArray); END; //Json Item list end; } |