Unable to make state readOnly using script if current user is from specific group

ahmadfaiz
Tera Contributor

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

5 REPLIES 5

manjusha_
Kilo Sage

@ahmadfaiz 

 

You should go with field level acl instead of client script.

Let me know if you need more help

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact

Thanks,

Manjusha Bangale

Ankur Bawiskar
Tera Patron
Tera Patron

@ahmadfaiz 

your script include should be client callable

your script include is wrong.

Please check this script and enhance it as per your requirement. It will be learning for you as well

var checkRecords = Class.create();
checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {

checkGroup: function(){

var user = this.getParameter('sysparm_userID');

var group = this.getParameter('sysparm_group');

return gs.getUser().getUserByID(user).isMemberOf(group);
},

type: 'checkRecords'
});

Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

var ga = new GlideAjax('checkRecords');
ga.addParam('sysparm_name', "checkGroup");
ga.addParam('sysparm_userID', g_form.getValue('caller_id'));
ga.addParam('sysparm_group', g_form.getValue('assignment_group'));
ga.getXMLAnswer(function(answer){
if(answer.toString() == 'false'){
g_form.clearValue('caller_id');
}
});

}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

ahmadfaiz_0-1693312727051.png

Getting this error, I am using this on different application, not on global

@ahmadfaiz 

please create the script include and onload client script in same scope as that of the table.

I just gave an example and mentioned to enhance it as per your requirement.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader