- I have to implement a bi-directional sync between Business Central ERP and Shopify.
- The sync has to be a scheduled one, not a real time one, and I have to optimise as much as I can to save API requests. The solution has to work on SaaS and I expect to have many requests to handle from both sides.
Just while I was getting depressed because I was finding no solutions, an online API course from Arend Jan Kauffmann showed as the icing on the cake and made me discover an unbelievable feature of API: the deltaLink and the deep inserts.
WHAT ARE ODATA DELTA LINKS AND WHY I USED THEM
The annoying thing about Business Central API is the current API usage limit.
When working in production environment the Http Error “429 Too Many Requests” was starting to be my nightmare. When dealing with synchronisation procedures this limit is a persistent problem.
Delta Links is a way to get deltas from a specific table. Basically when you get records you need to save a “delta link”, this delta link allows to retrieve only records that have been modified since previous requests.
First of all I created my custom API and I enable delta : ChangeTrackingAllowed = true;
This allows me to make a request with odata.track-changes http parameters.
All the magic is in the “@odata.deltaLink” URL.
Let’s supposed, after this initial request, I insert an item, update another one and delete the third. When calling again the provided odata.deltaLink I am going to get only the deltas as in picture:
Something really important is the delta links are “one time links”, it means that they allow one single requests and, after the request is served, they expire and you get a “410 Error Gone”. This means that sync mechanist must be implemented as “state of art” because if miss a request then the procedure has to reset from the initial request.
This is the way a deleted record is returned:
I just had to take to care about the way the insert action is managed in business central: it generates two events: an initial insert event and followed by an update event. This is by design.
DEEP INSERTS IN BUSINESS CENTRAL
In next article I will cover how to manage deep inserts and save API requests for SaaS.
1st of June, 2020