{Good to Know} Why you should never user USER_ROLES variable in your Dynamics CRM form scripts

Sometimes simple things can give you nightmares and the same happened with one of my colleague. So this is the scenario.

My colleague just needed to identify if a user is a particular security role and for that he was using the OOB script – Xrm.Page.context.getUserRoles(). The funny thing is that this method was returning him zero roles. After hours of cross checking, he finally turned up to me in frustration and we started debugging together.

For the first 15 mins, I was totally confused. Indeed Xrm.Page.context.getUserRoles() was returning an empty array. Finally I launched my favourite Chrome Dev Tools and started analyzing the method definition for the function. So I went ahead to the console of the Chrome developer tools and to check the method definition, instead of using Xrm.Page.context.getUserRoles(), I typed Xrm.Page.context.getUserRoles. Notice the difference here. I did not mention the parenthesis.

image 

 

And now you know. This function gets the value from the variable USER_ROLES. And my developer friend was having an array declared at the top of the form load script with exactly the same name. So this variable was getting initialized to an empty array and no wonder Xrm.Page.context.getUserRoles() was returning empty array.

Believe me, you would run out of search phrases in google to identify issues like this and may waste hours or days identifying the issue. Thanks to the wonderful developer tools these days in modern browsers, we were able to pinpoint to this issue in no time.

There are some other internal variables specific to user which CRM uses. Just good to know as this may come in handy.

image

 

Hope this helps!


Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe to get the latest posts sent to your email.

1 thought on “{Good to Know} Why you should never user USER_ROLES variable in your Dynamics CRM form scripts”

Comments are closed.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading