Show choice list option only to certain dept/groups and for rest user remove that option.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
13 hours ago
Hi all, so I have this task whereby in a select box choice options. The variable name of that select box for example is 'Service Request' and in there have 5 options. For option 1, 2 & 3 only users belonging to a certain department or certain groups get to see these options. If user not belonging to those exact department/groups they cannot see those options thereby only see option 4 & 5 only. I have tried in the script include and client script, but it's not working. Please help and assist me to get this to work. Much appreciated, thanks!
SCRIPT INCLUDE
var checkUserGroupDept = Class.create();
checkUserGroupDept.prototype = {
initialize: function() {},
getUserGroupDept: function() {
var userId = this.getParameter('sysparm_requester_name');
var userGR = new GlideRecord('sys_user');
userGR.addQuery('sys_id',userId);
gr.query();
if(userGR.get(userSysId)) {
var deptName = userGR.getDisplayValue('department');
if (deptName == 'Department A' || deptName == 'Department B') {
return true;
}
var gm = new GlideRecord('sys_user_grmember');
gm.addQuery('user',userSysId);
gm.query();
while (gm.next()) {
var grpName = gm.group.name.toString();
if (grpName == 'Group 1' || grpName == 'Group 2') {
return true;
}
}
return false;
}
},
type: 'checkUserGroupDept'
};
CLIENT SCRIPT
function onLoad() {
var ga = new GlideAjax('checkUserGroupDept');
ga.addParam('sysparm_name','getUserGroupDept');
ga.addParam('sysparm_userId', g_user.userID);
ga.getXMLAnswer(hideOption);
function hideOption(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'false'){
g_form.removeOption('service_request','option_1);
g_form.removeOption('service_request','option_2);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
7 hours ago
Hi @HanisahR ,
I see a couple of issues that are likely causing your code not to work. Let me walk through them and suggest some corrections:
Issues in Script Include
1. Parameter mismatch
In your getUserGroupDept function, you’re calling:
var userId = this.getParameter('sysparm_requester_name');
But in your Client Script, you’re passing:
ga.addParam('sysparm_userId', g_user.userID);
These don’t match. You should be retrieving sysparm_userId inside the Script Include.
2. Incorrect variable usage
- You’re using gr.query(); but the variable is actually userGR.
- You’re also calling userGR.get(userSysId) but userSysId isn’t defined anywhere. You already have the userId from the parameter, so you should use that.
3. Return values
GlideAjax can only return strings, so make sure you return "true" or "false" explicitly.
Fixed Script Include
var checkUserGroupDept = Class.create();
checkUserGroupDept.prototype = {
initialize: function() {},
getUserGroupDept: function() {
var userId = this.getParameter('sysparm_userId');
var userGR = new GlideRecord('sys_user');
if (userGR.get(userId)) {
var deptName = userGR.getDisplayValue('department');
if (deptName == 'Department A' || deptName == 'Department B') {
return "true";
}
var gm = new GlideRecord('sys_user_grmember');
gm.addQuery('user', userId);
gm.query();
while (gm.next()) {
var grpName = gm.group.name.toString();
if (grpName == 'Group 1' || grpName == 'Group 2') {
return "true";
}
}
}
return "false";
},
type: 'checkUserGroupDept'
};
Fixed Client Script
function onLoad() {
var ga = new GlideAjax('checkUserGroupDept');
ga.addParam('sysparm_name','getUserGroupDept');
ga.addParam('sysparm_userId', g_user.userID);
ga.getXMLAnswer(function(answer){
if (answer == 'false') {
g_form.removeOption('service_request','option_1');
g_form.removeOption('service_request','option_2');
g_form.removeOption('service_request','option_3');
}
});
}
Implementation Notes
- Always return strings from a Script Include used with GlideAjax ("true" / "false") since the response is text-based.
- Double-check the internal values of your choice options (not the labels) when using g_form.removeOption. The value might not be option_1 unless you explicitly configured it that way.
With these adjustments, the options should correctly be removed for users outside the specified departments/groups.
Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution so others can benefit as well.