
function setFieldSecurity(executionContext) {
var formContext = executionContext.getFormContext();
// Get current user roles
var userRoles = Xrm.Utility.getGlobalContext().userSettings.roles;
// Define the role that has permission to edit the field
var allowedRoleName = "System Administrator";
var hasAccess = false;
// Check if the current user has the allowed role
userRoles.forEach(function (role) {
if (role.name === allowedRoleName) {
hasAccess = true;
}
});
// Get the field and set it read-only if the user does not have access
var fieldName = "name"; // Change to your field name
if (!hasAccess) {
formContext.getControl(fieldName).setDisabled(true);
}
}