Hello
I'm trying to check if a user role is not in finance then disable all fields.
function ReadOnly() { var UserRole = Xrm.Page.context.getUserRoles().toString().replace("{", "").replace("}", ""); var check = "f0d793f3-9e84-dd11-a5c4-001ec9d279c2"; //finance GUID alert(UserRole +" "+ check); if (UserRole != check) { Xrm.Page.data.entity.attributes.forEach(function (attribute, index) { var control = Xrm.Page.getControl(attribute.getName()); if (control) { control.setDisabled(true) } }); } }
I tried adding a few steps to make sure that the UserRole was formatted correctly for me to compare the two strings and ive also included a alert to I could make sure that the values were being passed through correctly. I set my user role to finance for testing but despite the variables matching it is still disabling all fields.
Can someone please help ?
Thanks
Good day ns95,
Since my test account is also system administrator , so I modified some 'UserRole != check' as 'UserRole.indexOf(check)<0' to confirm whether the user role contains related role guid.
function ReadOnly() { var UserRole = Xrm.Page.context.getUserRoles().toString().replace("{", "").replace("}", ""); var check = "22045fce-a782-4bd7-b57d-d05103522d27"; //Sales Manger GUID alert(UserRole +" "+ check); if (UserRole.indexOf(check)<0) { Xrm.Page.data.entity.attributes.forEach ( function (attribute, index) { var control = Xrm.Page.getControl(attribute.getName()); if (control) { control.setDisabled(true); } } ); } }
The above script is nearly the same with yours. Since I don't have 'Finance' role so I changed it to 'Sales Manager'.
Scenario 1: Test Account without 'Sales Manager Role' , all fields locked as follow:
Scenario 2: Test Account with 'Sales Manager Role' , all fields are unlocked as follow(need to unlogin and clean the cache first):
Alet reports user role contain Sales Manager Roleid:
Successfully skip the Foreach disable logic.
So the coding should be normal. I would suggest you to validate your Finance role Guid and set breakpoint debugging to run the process.
Regards
Johnny