Introduction
With JavaScript, we have real-time interaction with Dynamics 365. Microsoft gives us feasibility to use CRM Web API in JavaScript to perform operations such as Create, Update, Delete, Retrieve, etc.
But there is a limitation on the number of API requests made by each user, per organization instance, within a five-minute sliding window. Please refer the below blog for more details:
https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/api-limits
This limit will cause the issue to the organization which is using a large number of requests for their application.
The application will throw “429” error which will give this type of message: “Combined execution time of incoming requests exceeded limit of 1,200,000 milliseconds over a time window of 300 seconds. Decrease the number of concurrent requests or reduce the duration of requests and try again later.”
Solution
To overcome this error, we have used JavaScript’s setTimeout() function. setTimeout() for single request works as expected but in order to use setTimeout() in for loop you need to add extra piece of code to get a proper result i.e. by using setTimeout() with IIFE (Immediately Invoked Function Expression).
var waitInterval = 300;
for (var i = 0; i < result.List.length; i++) {
(function (i) {
setTimeout(function () {
crmAPI.Create(entityName, record).then(function success(result){
});
}, waitInterval * i);
})(i);
}
You can change the waiting interval based on number of requests.
Conclusion
By using JavaScript’s setTimeout() with IIFE (Immediately Invoked Function Expression) we can overcome the CRM API limit.