JavaScript to Restrict Fields for Security Roles Microsoft Dynamics CRM
var Report = window.Report || {};
var users = [];
var emails = [];
// JavaScript source code
Report.ShowQylifyfield = function (executionContext) {
var formContext = executionContext. getFormContext();
if (formContext !== null) {
/// get Logged in user role Id
var userSettings = Xrm.Utility.getGlobalContext() .userSettings;
// Get Logged In User Security Roles
var loggedInUsersecurityRolesGuidA rray = userSettings.securityRoles;
var totalSecurityRolesArray = new Array();
//var arreyToCheck = [];
if ( loggedInUsersecurityRolesGuidA rray.length > 0) {
Xrm.WebApi.online. retrieveMultipleRecords("role" , "?$select=name,roleid").then(
function success(result) {
if (result.entities.length > 0) {
// Push Role Names and Role Ids to Array
for (var rolesCount = 0; rolesCount < result.entities.length; rolesCount++) {
totalSecurityRolesArray.push({ RoleName: result.entities[rolesCount]. name, RoleId: result.entities[rolesCount]. roleid });
}
// Compare the User Security Roles with Total Security Roles
for (var userSecurityRolesCounter = 0; userSecurityRolesCounter < loggedInUsersecurityRolesGuidA rray.length; userSecurityRolesCounter++) {
for (var totalsecurityRolesCounter = 0; totalsecurityRolesCounter < totalSecurityRolesArray. length; totalsecurityRolesCounter++) {
if (totalSecurityRolesArray[ totalsecurityRolesCounter]. RoleId.toLowerCase() === loggedInUsersecurityRolesGuidA rray[userSecurityRolesCounter] .toLowerCase()) {
if (totalSecurityRolesArray[ totalsecurityRolesCounter]. RoleName === 'Sales Manager') {
formContext.ui.controls.get(" cr935_lakshmantestfield"). setVisible(false); //your fieldname
return;
}
else {
formContext.ui.controls.get(" cr935_lakshmantestfield"). setVisible(true);. //Your Field Name
}
}
}
}
}
},
function (error) {
// Show error
Alert(error.message, null);
});
}
}
};
var users = [];
var emails = [];
// JavaScript source code
Report.ShowQylifyfield = function (executionContext) {
var formContext = executionContext.
if (formContext !== null) {
/// get Logged in user role Id
var userSettings = Xrm.Utility.getGlobalContext()
// Get Logged In User Security Roles
var loggedInUsersecurityRolesGuidA
var totalSecurityRolesArray = new Array();
//var arreyToCheck = [];
if (
Xrm.WebApi.online.
function success(result) {
if (result.entities.length > 0) {
// Push Role Names and Role Ids to Array
for (var rolesCount = 0; rolesCount < result.entities.length; rolesCount++) {
totalSecurityRolesArray.push({ RoleName: result.entities[rolesCount].
}
// Compare the User Security Roles with Total Security Roles
for (var userSecurityRolesCounter = 0; userSecurityRolesCounter < loggedInUsersecurityRolesGuidA
for (var totalsecurityRolesCounter = 0; totalsecurityRolesCounter < totalSecurityRolesArray.
if (totalSecurityRolesArray[
if (totalSecurityRolesArray[
formContext.ui.controls.get("
return;
}
else {
formContext.ui.controls.get("
}
}
}
}
}
},
function (error) {
// Show error
Alert(error.message, null);
});
}
}
};