Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Lock fields in MS CRM form using Security Role

(0) ShareShare
ReportReport
Posted on by 17

Hello Everyone,

I got a requirement to lock the fields in the form based on the security role . I wrote the code but somehow it is throwing an error . Please kindly help me 

Here is my code 

function OnLoad() {

if (UserHasRole("SecurityRole1") || UserHasRole("SecurityRole2")) {

Xrm.Page.getControl(“Fieldname1”).setDisabled(true);
Xrm.Page.getControl(“Fieldname2”).setDisabled(true);
Xrm.Page.getControl(“Fieldname3”).setDisabled(true);
Xrm.Page.getControl(“Fieldname4”).setDisabled(true);
Xrm.Page.getControl(“Fieldname5”).setDisabled(true);

} else {
Xrm.Page.getControl(“Fieldname1”).setDisabled(false);
Xrm.Page.getControl(“Fieldname2”).setDisabled(false);
Xrm.Page.getControl(“Fieldname3”).setDisabled(false);
Xrm.Page.getControl(“Fieldname4”).setDisabled(false);
Xrm.Page.getControl(“Fieldname5”).setDisabled(false);

}
}


function UserHasRole(roleName) {

var serverUrl = Xrm.Page.context.getServerUrl();
var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/";

oDataEndpointUrl += "RoleSet?$top=1&$filter=Name eq '" + roleName + "'";

var service = GetRequestObject();

if (service != null) {

service.open("GET", oDataEndpointUrl, false);
service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
service.setRequestHeader("Accept", "application/json, text/javascript, */*");
service.send(null);

var requestResults = eval('(' + service.responseText + ')').d;

if (requestResults != null && requestResults.results.length == 1) {
var role = requestResults.results[0];

var id = role.RoleId;

var currentUserRoles = Xrm.Page.context.getUserRoles();

for (var i = 0; i < currentUserRoles.length; i++) {
var userRole = currentUserRoles[i];
if (GuidsAreEqual(userRole, id)) {
return true;
}
}
}
}

return false;
}

function GetRequestObject() {
if (window.XMLHttpRequest) {
return new window.XMLHttpRequest;
} else {
try {
return new ActiveXObject("MSXML2.XMLHTTP.3.0");
} catch (ex) {
return null;
}
}
}

function GuidsAreEqual(guid1, guid2) {
var isEqual = false;

if (guid1 == null || guid2 == null) {
isEqual = false;
} else {
isEqual = guid1.replace(/[{}]/g, "").toLowerCase() == guid2.replace(/[{}]/g, "").toLowerCase();
}

return isEqual;
}

Calling the function on Form Onload 

Error I am getting : SyntaxError: Unexpected token (
at RunHandlerInternal

 Please Kindly Help me 

*This post is locked for comments

  • LeoAlt Profile Picture
    LeoAlt 16,331 on at
    RE: Lock fields in MS CRM form using Security Role

    Hi Bhavya,

    Your logic seems good, I copied whole of your code and executed it sucessfully in CRM.

    Have you tried format code in any editor, like online playcode.io or VS code?

    The only problem I've met is that when I directly copied and pasted code into playcode.io, I got quota error of field:

    961.jpg

    Could you get id value of RoleId successfully? 

     962.jpg

    Once you could got the value, the following steps shall be ok to end,

    and try to add another break point into GuidsAreEqual function to test whether it could return true when any of current user's roleid equals the retrieved id result:

    963.jpg

    Finally, as Guido suggested, you could consider work with Field Level Security to block field rather than JS.:

     964.jpg

    965.jpg

    However, some managed system fields could not be set this property to be locked, while JS still could, 

    so try to debug different functions in your code to solve the issue and work with JS to meet your real requirements.

    Best Regards,

    Leo

  • Guido Preite Profile Picture
    Guido Preite 54,073 Moderator on at
    RE: Lock fields in MS CRM form using Security Role

    you should use Field Level Security instead of checking the roles by JavaScript

  • Suggested answer
    Prashant_ Profile Picture
    Prashant_ 1,040 on at
    RE: Lock fields in MS CRM form using Security Role

    Try to debug your code.You will narrow down issue while debugging.use following link 

    https://cloudblogs.microsoft.com/dynamics365/no-audience/2015/11/29/debugging-custom-javascript-code-in-crm-using-browser-developer-tools/

    It seems like you might have copy pasted code.Remove double quote and add again in above code.Some times double copy paste operation change actual double quote to different sign and give unexpected token error.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,387 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans