{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!

Advertisements

About Debajit
I am a Dynamics CRM Most Valuable Professional (MVP) with 10 years of experience in Microsoft .NET Technologies and 7 years of dedicated experience in Microsoft Dynamics CRM. I have worked with companies like Microsoft, SanDisk, PwC, TMF Group and have extensive experience of implementing complex CRM solutions from both offshore and client side. Currently the face of XrmForYou.com with significant experience in delivering corporate training on Dynamics CRM and have already delivered multiple projects to client through XrmForYou.com Author of multiple tools on codeplex including the 'Role Based Views' and 'CRM-Sharepoint Metadata manager & Attachment Extractor' which are available for commercial use under XrmForYou.com For consulting/ training, drop me a note at info@xrmforyou.com or visit our website www.xrmforyou.com

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

  1. Great to know. This kind of error could have indeed wasted days. Since internal variables are not even documented, this is a great find.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: