Unable to make state readOnly using script if current user is from specific group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â08-29-2023 03:02 AM - edited â08-29-2023 03:07 AM
Client Script
function onLoad() {
// Check the user's assignment group membership
var assignmentGroupName = 'Your Assignment Group Name';
var userSysId = g_user.userID;
var ga = new GlideAjax('CheckUserInGroup');
ga.addParam('sysparm_name', 'userInGroup');
ga.addParam('sysparm_user', userSysId);
ga.addParam('sysparm_group', assignmentGroupName);
ga.getXML(updateFieldAttributes);
}
function updateFieldAttributes(response) {
var responseObj = response.responseXML.documentElement;
var isInGroup = responseObj.getAttribute('isInGroup');
if (isInGroup === 'true') {
// User is in the assignment group, make the field read/write
g_form.setReadWrite('field_name', true);
} else {
// User is not in the assignment group, make the field read-only
g_form.setReadOnly('field_name', true);
}
}
Script Include
var CheckUserInGroup = Class.create();
CheckUserInGroup.prototype = {
userInGroup: function() {
var userSysId = this.getParameter('sysparm_user');
var groupName = this.getParameter('sysparm_group');
var user = new GlideUser(userSysId);
var userGroups = user.getMyGroups();
var isInGroup = false;
userGroups.forEach(function(group) {
if (group.getName() === groupName) {
isInGroup = true;
}
});
return new XMLAnswer(isInGroup.toString());
},
type: 'CheckUserInGroup'
};
This script is not working, I have changed the field_name and group with needed group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â08-29-2023 03:31 AM
Hi
Looks like your script include is client callable, amongst other issues.
Use this
Script include, make sure its client callable
var CheckUserInGroup = Class.create();
CheckUserInGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
userInGroup: function() {
var groupName = this.getParameter('sysparm_group');
return gs.getUser().isMemberOf(''+groupName);
},
type: 'CheckUserInGroup'
});
Client script
function onLoad() {
var assignmentGroupName = 'Team Development Code Reviewers';
var userSysId = g_user.userID;
alert(userSysId );
var ga = new GlideAjax('CheckUserInGroup');
ga.addParam('sysparm_name', 'userInGroup');
ga.addParam('sysparm_user', userSysId);
ga.addParam('sysparm_group', assignmentGroupName);
ga.getXML(updateFieldAttributes);
}
function updateFieldAttributes(response) {
var responseObj = response.responseXML.documentElement.getAttribute("answer");
alert(responseObj );
if (responseObj == true) {
// User is in the assignment group, make the field read/write
g_form.setReadWrite('field_name', true);
} else {
// User is not in the assignment group, make the field read-only
g_form.setReadOnly('field_name', true);
}
}