CRM Crate

Understanding the JavaScript shared variables in Dynamics 365 CRM

We will learn & understand the JavaScript shared variables in Dynamics 365. Before we start, make sure to subscribe to CRM Crate so that you can stay up to date in the field of Dynamics 365.

Understanding the JavaScript shared variables in Dynamics 365

What are client-side Shared Variables in Dynamics 365?

We can create & retrieve a common shared variable within the form execution context of Dynamics 365. This means that we can set & get a shared variable whenever a JavaScript function is called using the form’s event handler.

What is the use of Shared Variables in Dynamics 365?

We can use the Shared Variables in-order to store the global values while performing an asynchronous operation. For example, we will create and store a value in a new Shared Variable and execute an asynchronous HTTP request. Later, we will consume the earlier created Shared Variable value on receiving the HTTP response.

Using Web Client API for creating & retrieving the Shared Variables

We will use the “getSharedVariable” and “setSharedVariable” client API for initializing & consuming the Shared Variables.

ExecutionContextObj.setSharedVariable(key, value)

Sets the value of a variable to be used by a handler after the current handler completes.

ExecutionContextObj.getSharedVariable(key)

Retrieves a variable set using the setSharedVariable method.

JavaScript snippet for creating & consuming the Shared Variables.

The below given JavaScript function is called during an “On-Change” event of form’s attribute. We have initialized the Shared Variable and execute a HTTP request which is asynchronous in nature. As soon as we initialize the Shared Variable, it gets added in the form’s execution context and is easily retrieved on receiving a HTTP reponse.

function CallMethod (executionContext) {
var formContext = executionContext.getFormContext();

//Get Form's Field Value.
var ShareVariable = formContext.getAttribute("cc_sharedvariable").getValue();

//Set The  Shared Variable
executionContext.setSharedVariable("ShareVariableKey", ShareVariable);

//Call Async HTTP Request
    Xrm.Utility.showProgressIndicator("CRM Crate - Ongoing Async Opertaion!");
var WebURL = "https://prod-05.centralindia.logic.azure.com:443/workflows/d31912727754221b5689b5a95242456/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=c5tVrJvZiEIXQU49e-U3f4NEuR0YMFFGvaO-fCPfwHw";
    var input = JSON.stringify({ Input: "Test"});
    var req = new XMLHttpRequest();
    req.open("POST", WebURL, true);
    req.setRequestHeader('Content-Type', 'application/json');
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
			                Xrm.Utility.closeProgressIndicator();
                var result = this.response;
				
//Get The Shared Variable
var getSV = executionContext.getSharedVariable("ShareVariableKey");

//Display The Retrieved Shared Variable In A Dialog Box
var alertStrings = { confirmButtonLabel: "Okay", text: "Shared Variable = " + getSV};
var alertOptions = { height: 120, width: 260 };
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(
    function (success) {
        console.log("Alert dialog closed");
    },
    function (error) {
        console.log(error.message);
    }
);
formContext.getAttribute("cc_finaloutput").setValue(getSV);

            }
            else if (this.status === 400) {
			                Xrm.Utility.closeProgressIndicator();
                var result = this.response;
            }
        }
    };
    req.send(input);

}

Validate the changes in Dynamics 365

Once the above JavaScript snippet is configured & deployed, navigate to the D365 and validate the Shared Variable implementation as shown below.

Thus, we learned the use of the Shared Variables in Dynamics 365 CRM.

5 3 votes
Article Rating
Subscribe
Notify of
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
error: CRM Crate Security Engine - Disabled Right Click & Selection!

Congratulations!

Well Done,
Welcome to CRM Crate

Stay tuned with us and get all latest updates and learning in Microsoft CRM and related techonologes.