Seamless Integration: How to Sync Business Central with External Systems Instantly or in Batches
In today’s fast-paced business world, integrating your ERP system (like Business Central) with other external systems is crucial for streamlining processes and ensuring data consistency. However, if you’re new to API integrations or struggling with how to send data from Business Central to another system, don’t worry!
In this post, I am going to walk you through the process of sending data from Business Central to an external system using APIs. By the end of this guide, you’ll have a clear understanding of how to perform integrations smoothly, without the complexity.
Steps to Achieve Goal:
You can easily send data from Business Central to an external system by calling the link set in General Ledger Setup. Below is the logic for sending data via an API.
You can encapsulate this logic inside a Codeunit, and call it as needed based on your synchronization requirements:
Real-Time Data Sync:
If you want the data to be synced in real-time (for example, as soon as new data is entered into Business Central), you can call the procedure within the OnAfterInsert() trigger. This ensures that every time a new record is created, it automatically triggers the procedure to send the data.
trigger OnAfterInsert()
begin
SendPostRequest(Rec);
end;
Batch Data Sync:
If you prefer to sync the data at the end of a batch process (for example, at the end of the day), you can loop through the records using FindSet() and then call the procedure inside the loop. This will send data in bulk at a scheduled time rather than in real-time.
procedure SyncDataInBatch()
var
Rec_SO: Record “Sales Header”;
begin
Rec_SO.setrange(CreatedAt,today()); // Apply any filter as per your need.
if Rec_SO.FindSet() then
repeat
SendPostRequest(Rec_SO);
until Rec_SO.Next() = 0;
end;
// Below is the logic for posting data from BC to Third Party Applications via API
procedure SendPostRequest(var Rec_SO: Record “Sales Header”)
var
HttpClient: HttpClient;
HttpContent: HttpContent;
HttpResponseMessage: HttpResponseMessage;
HttpRequestMessage: HttpRequestMessage;
JsonObject: JsonObject;
JsonText: Text;
Rec_GLE: Record “General Ledger Setup”;
contentHeaders: HttpHeaders;
OutPutString: Text;
begin
Rec_GLE.Get();
Rec_GLE.TestField(“API Link”); // where the other system API link has been stored and we are using via AL
HttpRequestMessage.SetRequestUri(Rec_GLE.”API Link”);
HttpRequestMessage.Method := ‘POST’;
JsonObject.Add(‘system_id’, Rec_SO.SystemId); // Passing Sales Header System ID(GUID)
JsonObject.Add(‘document_number’, Rec_SO.”No.”); // Passing Sales Header No
JsonObject.Add(‘type’, ‘ReleasedSalesInvoice’); // Passing Sales Header type
JsonObject.WriteTo(JsonText);
HttpContent.WriteFrom(JsonText);
HttpContent.GetHeaders(contentHeaders);
contentHeaders.Add(‘charset’, ‘UTF-8’);
contentHeaders.Remove(‘Content-Type’);
contentHeaders.Add(‘Content-Type’, ‘application/json; charset=utf-8’);
HttpRequestMessage.Content(HttpContent);
if HttpClient.Send(HttpRequestMessage, HttpResponseMessage) then
if HttpResponseMessage.IsSuccessStatusCode then begin
HttpResponseMessage.Content.ReadAs(OutPutString);
Message(‘%1’, OutPutString);
end
else
Error(‘Error %1’, HttpResponseMessage.ReasonPhrase);
end;
Conclusion:
To conclude, sending data between Business Central and other systems is not as complicated as it might seem. By following the steps outlined above, you’ll be able to create smooth, efficient integrations that will save time, reduce errors, and improve your business processes.
Happy Coding!
We hope you found this blog useful, and if you would like to discuss anything, you can reach out to us at transform@cloudfonts.com.