web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Regarding - API integration to transfer data to third party applications.

(0) ShareShare
ReportReport
Posted on by 190

Hi all 

As my Requirement is :

 I have to share business central data to Thirdparty Application using API.
I want to Create a codeunit that calls the URL and maps the fields to ThirdParty Application.

 As I have done acquiring data from Business central and I've stuck in the part mapping the data to 3rd party Application directly using API.

Anybody can help me to come out of this is ...

I have the same question (0)
  • Suggested answer
    IB-29041624-0 Profile Picture
    1,191 Moderator on at

    This is pretty easy when you have the documentation for the API you are trying to access. Especially if it is a json/rest api,

    AL have a httpclient and support for json.

    You can read more about the http client here: docs.microsoft.com/.../httpclient-data-type

  • karthikeyani c Profile Picture
    190 on at

    Yes Sir,

    But our  Business central and 3rd Party Applicaton  has different json format. So we are stuck on  mapping business central fields to 3rd Party.

    I need a kind of mapping method to map two different Json Formats using AL code.

  • Suggested answer
    IB-29041624-0 Profile Picture
    1,191 Moderator on at
    [quote user="karthikeyani c"]

    Yes Sir,

    But our  Business central and 3rd Party Applicaton  has different json format. So we are stuck on  mapping business central fields to 3rd Party.

    I need a kind of mapping method to map two different Json Formats using AL code.

    [/quote]

    Well what you normally would do is to look at the 3.party API request and figure out what data from the BC database you should put into that request and than build the 3.party request json request. Then you send that request Json in a Http content object.

    And then handle the response you get from the 3.party (usually in form of a http response object formatted as json), process the response json and then possible update your BC data if required.

    If the 3.party have a public API maybe you can share here what API you are trying to process?

  • karthikeyani c Profile Picture
    190 on at

    Hi Sir,

    In the below coding we have retrieved data using Get Method. Then I put that response in Http Content Object.

    When I'm using Post method to transfer data from Business Central to 3rd Party , there I received error 'Bad Request'.

    codeunit 50137 apiconnect

    {

       procedure APIConnect()

       var

           Client: HttpClient;

           Response: HttpResponseMessage;

           Request: HttpRequestMessage;

           Header: HttpHeaders;

           Result: Text;

           Content: HttpContent;

           newClient: HttpClient;

           newResponse: HttpResponseMessage;

           newRequest: HttpRequestMessage;

           newHeader: HttpHeaders;

           newURL: Text;

           Output: Text;

           newContent: HttpContent;

       begin

           Header := Client.DefaultRequestHeaders;

           //Header.Add('User-Agent', 'Dynamic 365 Business Central');

           Header.Add('Authorization', 'Basic xxx-//');

           Request.SetRequestUri('xxx - // this is BC url');

           Request.Method('Get');

           Client.Send(Request, Response);

           if Response.IsSuccessStatusCode then

               Content := Response.Content

           else

               Message('Response was negative %1,%2', Response.HttpStatusCode, Response.ReasonPhrase);

           Content.ReadAs(Result);

           Message(Result);

           //post method

           newRequest.Method('POST');

           newRequest.SetRequestUri('xxx 3rd party url');

           newRequest.GetHeaders(newHeader);

           newHeader.Add('accept', 'application/json');

           newHeader.Add('Authorization', 'Token xxx');

           newContent.WriteFrom(Result);

           newContent.GetHeaders(newHeader);

           newHeader.Remove('Content-Type');

           newHeader.Add('Content-Type', 'application/json');

           newRequest.Content := newContent;

           newClient.Send(newRequest, newResponse);

           if newResponse.IsSuccessStatusCode then

               newContent := newResponse.Content

           else

               Message('Response was negative %1,%2', newResponse.HttpStatusCode, newResponse.ReasonPhrase);

       end;

    }

    Below the coding are specified for 3rdParty Request Body.

    {

    "metadata": {

      "open": "true",

      "archived": "false",

       "tags": []

      },

    "mode": {

      "id": 1,

      "code": "FTL",

      "description": "Full Truckload"

      },

    "equipment_type": {

       "id": 1,

       "name": "Dry Van",

       "machine_readable": "DRY_VAN"

    },

    "service_level": {

       "code": "STD",

       "id": "19"

    },

    "stops": [

       {

           "ordinal_index": 0,

           "is_pickup": "true",

           "location": {

               "location_name": "Pickup Company",

               "address": {

                  "address_1": "515 Congress Ave",

                  "city": "Austin",

                  "state_province": "TX",

                  "postal_code": "78701",

                  "country": "US"

                  },    

              "location_type": {

                      "id": "1",

                      "name": "Business (with dock or forklift)"

                  },

              "point_of_contacts": [

                      {

                          "preferences": {

                               "cancellation": false,

                               "delayed": false,

                               "delivered": false,

                               "eta_changed": false

                          },      

                          "email": "nathaniel.archer@gmail.com",

                          "first_name": "Nathaniel",

                          "last_name": "Archer",

                          "phone_number": "5122335667"

                      }

                  ]

              },

          "planned_date": "2020-12-24",

          "planned_time_window_end": "10:00:00",

          "planned_time_window_start": "08:00:00"

          },

          {

              "ordinal_index": 1,

              "is_dropoff": "true",

              "location": {

                  "location_name": "Dropoff Company",

                  "address": {

                      "address_1": "5100 Congress Ave",

                      "city": "Austin",

                      "state_province": "TX",

                      "postal_code": "78701",

                      "country": "US"

                  },

                  "location_type": {

                      "id": "1",

                      "name": "Business (with dock or forklift)"

                  },

                  "point_of_contacts": [

                      {

                          "preferences": {

                               "cancellation": false,

                               "delayed": false,

                               "delivered": false,

                               "eta_changed": false

                          },      

                          "email": "nathaniel.archer3@s.gmail.com",

                          "first_name": "Nathaniel",

                          "last_name": "Archer",

                          "phone_number": "5122335668"

                      }

                  ]

              },

              "planned_date": "2020-12-25",

              "planned_time_window_end": "10:00:00",

              "planned_time_window_start": "08:00:00"

          }

        ],      

        "line_items": [

          {

           "description": "laptops",

           "total_packages": "10",

           "package_weight": "6890",

           "weight_unit": "LB"

          }

        ]  

    }

    Below the is a json format of Bc's Data.

    {

       "@odata.context": " xxxxxxx -//",

       "value": [

           {

               "@odata.etag": "W/\"JzIwOzExOTI3NDY3NDA0MTM5MzU5Njg2MTswMDsn\"",

               "systemId": "xxx -//",

               "open": "FTL",

               "code": "DRY VAN",

               "name": "STD",

               "address": "AUSTIN",

               "email": "NEW@GMAIL.COM",

               "phoneNumber": 97490960,

               "description": "PEN",

               "totalPackages": 1,

               "address_1": "5100 CONGRESS AVE",

               "state_province": "US",

               "postal_code": "9887",

               "country": "US",

               "location_name": "DROPOFF COMPANY"

           }

       ]

    }

  • Suggested answer
    IB-29041624-0 Profile Picture
    1,191 Moderator on at

    I don't see the code where you build the Json content, but you have checked that the Json you send match the requirements?

  • karthikeyani c Profile Picture
    190 on at

    Hi sir ,

    My Requirement is :

    I'm trying to connect two Applications , one is Business Central ,another one is (Some 3rdParty App)

    Both Applications has each different Json Format on their own. In that case I'm Stuck in the part to map the fields from Business Central to 3rd Party Applications.

    So, I need mapping method to map the fields from one to another using AL CODE.

  • Suggested answer
    IB-29041624-0 Profile Picture
    1,191 Moderator on at

    Consignment.Add('ConsignmentNo', '');
                            Consignment.Add('ReturningLabel', true);
                            Consignment.Add('Consignor', ConsignorId);
                            Consignment.Add('Carrier', ShippingAgent);
                            Consignment.add('OrderNo', rec."No.");
                            Consignment.Add('Product', ShippingAgentServices.Code);
                            Consignment.add('Remark', rec."External Document No.");
                            Consignment.Add('RecipientCustomerNo', rec."Sell-to Customer No.");
                            Consignment.add('RecipientName', rec."Ship-to Name");
                            Consignment.Add('RecipientAddress', rec."Ship-to Address");
                            Consignment.Add('RecipientAddress2', rec."Ship-to Address 2");
                            if rec."Ship-to Country/Region Code" = '' then rec."Ship-to Country/Region Code" := 'NO';
                            Consignment.add('RecipientCountryCode', rec."Ship-to Country/Region Code");
                            Consignment.add('RecipientZipCode', rec."Ship-to Post Code");
                            Consignment.add('RecipientPlace', rec."Ship-to City");
                            Consignment.add('RecipientPhoneNo', rec."Sell-to Phone No.");
                            Consignment.add('RecipientCellphoneNo', rec."Sell-to Phone No.");
                            Consignment.add('RecipientReference', rec."Your Reference");
                            Consignment.Add('RecipientEmail', rec."Sell-to E-Mail");
                            Consignment.Add('InvoiceName', rec."Bill-to Name");
                            Consignment.Add('InvoiceAddress', rec."Bill-to Address");
                            Consignment.Add('InvoiceCountryCode', rec."Bill-to Country/Region Code");
                            Consignment.Add('InvoiceZipCode', rec."Bill-to Post Code");
                            Consignment.Add('InvoicePlace', rec."Bill-to City");
                            Consignment.Add('DeliveryInstruction', '');
                            Consignment.add('GiroAmount', 0);
                            Consignment.Add('GiroCurrency', '');
                            Consignment.add('CustomerKode', '');
                            Consignment.Add('LoadCarrierId', '');
                            Consignment.Add('ServiceDropId', '');
                            Consignment.Add('TerminalCode', '');
                            Consignment.Add('PrivateDeliveryCode', '');
                            Consignment.Add('InsuranceText', '');
                            Consignment.Add('ServiceCode', 0);
                            Consignment.add('CreateReturn', false);
                            Consignment.Add('ReturnProduct', '');
                            Consignment.Add('TerminalCodeSchenker', '

    Here you have a sample where Consignment is a Json object and rec in this sample is the sales order.

    I am using the Json object in AL

    docs.microsoft.com/.../jsonobject-data-type

  • karthikeyani c Profile Picture
    190 on at

    Yes ,

    I have done it

    But I got an internal server error in 3rd Party Application when I run this coding .

    Kindly help me to come out of this issue....

  • Suggested answer
    IB-29041624-0 Profile Picture
    1,191 Moderator on at

    It is not easy to help you further here without a complete description of the API you are trying to communicate with.

    You should contact the 3.party and ask them for an explanation about why your request generates and error on their side. They should be able to tell you what is wrong with your request.

    If we get a more specific error, then it is easier to help you further.

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,334 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,717 Super User 2026 Season 1

#3
AndrewThomas81 Profile Picture

AndrewThomas81 1,470

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans