POLLING IN DYNAMICS CRM USING setTimeout() in JS
As a CRM developer, we have encountered situations where we needs to wait for a fixed interval of time or wait for an event to occur in order to execute our JS code. In computer terms, this term is often referred as “Polling”. The actual definition of polling in CRM terms can be interpreted as “Actively sampling the status of a field or CRM record inside our java script as a synchronous activity before executing the main algorithm in JS”.
You may wonder what is the necessity of performing polling activity inside Dynamics CRM? Well the main purpose of this blog is to unveil that mystery and deep dive into some real time examples of its usage.
Business Requirement #1: When we open a new email/trying to create a new one, the from field in the new email always gets defaulted to one of the service account in CRM. The requirement is to default the from email field to logged-in users name. Initially, we wrote a java script On Load event and check if the form type equals 1, then populate the from field with current user. But our value is being over written by the CRM default service account. Therefore we need to insert polling into our JS when we need to wait and check until the CRM populates the default value and then overwrite it with logged-in user.
Since the time taken to populate a default value by CRM is always fixed, we don’t see any necessity in using polling (which is apt if the time interval is variable). Therefore, we just did some trial & error method and used JS native setTimeout() with a time out value of 1000ms. The code is :
setTimeout( function() { //Write your logic },1000);
The logic will get executed after a delay for 1000ms and we are able to over write the Dynamics CRM value.
Business Requirement #2: On click of a custom button, we need to change the status of opportunity record and move the BPF to next stage. Wonder where does polling comes into picture in this requirement? After CRM 2015 roll-up, if you want to advance to next stage in BPF, the record should not be dirty. Meaning you have to first save the unsaved changes and then will be able to advance to next BPF stage. Inside JS, we need to continuously poll the opportunity record dirty status and once it gets saved we can move BPF to next stage. The following are the 2 code templates you can use in JS for polling an event in CRM:
function moveNextStage(currentStage){ var pollingNumber = 10; var intervalId; intervalId = setInterval(function () { pollingNumber -= 1; if (Xrm.Page.data.process.getActiveStage().getName() != currentStage) { clearInterval(intervalId); } if (!Xrm.Page.data.entity.getIsDirty() && Xrm.Page.data.process.getActiveStage().getName() == currentStage) { Xrm.Page.data.process.moveNext(function (result) { if (result != null) { // Success handle } }); pollingNumber = 0; clearInterval(intervalId); } if (pollingNumber <= 0) { clearInterval(intervalId); } }, 300); }
This function accepts currentStage as input parameter and validates if it equals to active stage of BPF. The polling algorithm has a time interval of 300ms and will poll 10 times to check if the record is not dirty. The polling parameters should be adjusted as per the business requirement and it is recommended to set at a higher threshold.
You can alternatively use below algorithm as well for polling:
function _polling (callback) { setTimeout (function () { If ( !Xrm.Page.data.entity.getIsDirty()) return callback(); _polling (callback); },500); } function _moveNextStage() { _polling ( function () { Console.log(“ Form save is completed”); //write your code to move BPF stage }); }
In the above algorithm, the time interval is fixed at 500 ms and the polling number is set to infinity. So this code will execute forever at intervals of 500ms until the form is saved. After being saved, we can execute the JS script to move BPF to next stage.
*This post is locked for comments