Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at info@xrmforyou.com
These functions have been introduced way back in 2011 but till date I believe this is one of the least used features of Dynamics 365 Client API’s. And partly because of the fact their limited scope when it comes to sharing variables.
I wonder many may have tried it but had to move over to other methods because these methods didn’t meet their needs.
Nonetheless I get this question every time I do a technical training on Dynamics 365 – What are these API’s used for. So I decided to dedicate a blog for this. So let’s start with the Microsoft definition of setSharedVariable function to understand the scope.
“Sets the value of a variable to be used by a handler after the current handler completes.”
The first impression you get from this definition is – if you have multiple handlers for the onload event of a form and if I set a variable using setSharedVariable method, the other handlers for the same event shall get the value using getSharedVariable function.
function pageLoad(e) {
console.log("Inside Page Load.");
e.setSharedVariable("key1", "From pageLoad function.");
console.log(e.getSharedVariable("key1"));
}
function f1(e) {
console.log(e.getSharedVariable("key1"));
e.setSharedVariable("key1", "From f1 function.");
}
function f2(e) {
console.log(e.getSharedVariable("key1"));
}
So let’s look at the code above. We have three methods – pageLoad, f1 & f2. All these three functions I have registered on the onload event in order. So basically these are three handlers for the same onload event.
And then I set a variable using setSharedVariable function.
Now as you can see, I am trying to get the same value in functions f1 & f2.
Let’s see what’s the output.
As you can see, the shared variable is available inside the method pageLoad. However it is not available for functions f1 & f2.
So it’s kind of heartbreaking right! So where it is useful and what is the scope of the variable using setSharedvariable function.
I change my pageLoad function to below. As you can see, I am calling an async function to get the record details of an account. By the time, the account details are retrieved, the function have exited. However on-success of retrieve method I am trying to access the value back.
function pageLoad(e) {
console.log("Inside Page Load.");
e.setSharedVariable("key1", "From pageLoad function.");
Xrm.WebApi.retrieveRecord("account", "a8a19cdd-88df-e311-b8e5-6c3be5a8b200", "?$select=name,revenue").then(
function success(result) {
console.log("Retrieved values: Name: " + result.name + ", Revenue: " + result.revenue);
console.log("Printing Shared variable: ", e.getSharedVariable("key1"));
},
function (error) {
console.log(error.message);
// handle error conditions
}
);}
And here is the output in the console. As you can see, I am still able to access this variable.
So if you have some async/ callback functions, these methods will work good for you. However if you trying to persist values across lifetime of the form or even pass data to multiple handlers of the same event, this does not work.
That limits the scope to a great extent. And now I guess you understand why it is used pretty less.
Hope this helps!
Debajit Dutta
(Microsoft MVP)
Discover more from Debajit's Power Apps & Dynamics 365 Blog
Subscribe to get the latest posts sent to your email.