- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-12-2025 02:02 AM
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);
}
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2025 02:59 AM
Found out that these scripts worked. Would like to thank everyone who contributed to replying to this question, much appreciated!
SCRIPT INCLUDE // Client callable true, application Global
var checkUserGroupDept = Class.create();
checkUserGroupDept.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getGroupDept: function() {
var userID = this.getParameter('sysparm_sysid');
var userGR = new GlideRecord('sys_user');
if (userGR.get(userID)) {
var dept = userGR.getValue('department');
var inGroup = false;
var groupUser = new GlideRecord('sys_user_grmember');
groupUser.addQuery('group', 'sysid1'); // sysid group1
groupUser.addQuery('group', 'sysid2'); // sysid group2
groupUser.query();
while (groupUser.next()) {
if (groupUser.user == userID) {
inGroup = true;
break;
}
}
// Check if user is NOT in group OR department is NOT dept1 or dept2
if (!inGroup && (dept !== 'dept1' && dept !== 'dept2)) { // sysid dept1, dept2
return 'true';
}
}
return 'false';
}
});^^ For some reason the if condition works when && is used instead of ||
CLIENT SCRIPT
function onLoad() {
// get current user's sys_id
var sysid = g_user.userID;
// call Script Include via GlideAjax
var ga = new GlideAjax('checkUserGroupDept'); // Script Include name
ga.addParam('sysparm_name', 'getGroupDept'); // Function name in Script Include
ga.addParam('sysparm_sysid', sysid); // pass user sys_id
ga.getXMLAnswer(parseResponse);
function parseResponse(answer) {
if (answer == 'true') {
g_form.removeOption('service_request', 'value_option1', 'Name Option1');
g_form.removeOption('service_request', 'value_option2', 'Name Option2');
g_form.removeOption('service_request', 'value_option3', 'Name Option3');
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-15-2025 02:59 AM
Hi @HanisahR ,
If the options are still accessible, here are a few things to check:
Choice values
g_form.removeOption() uses the Value field, not the Label. Double-check the variable’s option values in the catalog item.
Client-callable
Ensure your Script Include is marked as Client Callable.
Debug the return
Add gs.info(answer); in your client script to confirm you’re actually getting "true" / "false".
Verify variable name
Make sure the variable is really called service_request in the catalog item (case-sensitive).
If the log shows "false" but the options don’t disappear, it’s almost always a mismatch between the option value you’re passing and what’s configured on the variable.
Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution so others can benefit as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-14-2025 12:35 AM - edited ‎09-14-2025 12:44 AM
There is no variable defined as userSysId in your script include.
if(userGR.get(userSysId)) {
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-14-2025 01:01 AM - edited ‎09-14-2025 01:03 AM
g_form.removeOption('choices field name', 'VALUE_TO_REMOVE');your value, option 1 is not in quotes.
next issue I can find is your are returning false, while checking answer == 'false', it should be answer == false.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-14-2025 02:27 AM
Step-by-Step Solution1. Identify User's Department or GroupFetch the current user's department or group with the platform's user context (often via session variable, API, or a profile data resource).Use fields like current.user.department or the roles/groups array as your identifier�.2. Filter the Select Box Options DynamicallyDefine all options for 'Service Request'.Tag each option with allowed departments/groups.Use a client script or dynamic dropdown data filter to conditionally display options.Example implementation:const userDept = getUserDepartment(); // fetch from session or data binding
const allOptions = [
{ label: 'Option 1', value: '1', allowedDept: ['HR', 'Finance'] },
{ label: 'Option 2', value: '2', allowedDept: ['HR', 'Finance'] },
{ label: 'Option 3', value: '3', allowedDept: ['HR'] },
{ label: 'Option 4', value: '4', allowedDept: ['All'] },
{ label: 'Option 5', value: '5', allowedDept: ['All'] }
];
const visibleOptions = allOptions.filter(
option => option.allowedDept.includes(userDept) || option.allowedDept.includes('All')
);
// Bind visibleOptions to your select box componentIn ServiceNow, you may also use:g_form.removeOption('service_request', '1');
g_form.removeOption('service_request', '2');
g_form.removeOption('service_request', '3');Only if the user's department doesn't match the allowed ones�.3. Remove Option for Other UsersOn page load or relevant form event, execute the filtering logic to ensure only authorized users see Options 1, 2, and 3�.The remaining users only see Option 4 and 5.Important NotesThis logic works for both UI Builder record pages or catalog item forms if integrated via client script or via a data resource in the dropdown component�.For catalog items, similar control can be enforced with Catalog Client Scripts in ServiceNow.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2025 02:59 AM
Found out that these scripts worked. Would like to thank everyone who contributed to replying to this question, much appreciated!
SCRIPT INCLUDE // Client callable true, application Global
var checkUserGroupDept = Class.create();
checkUserGroupDept.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getGroupDept: function() {
var userID = this.getParameter('sysparm_sysid');
var userGR = new GlideRecord('sys_user');
if (userGR.get(userID)) {
var dept = userGR.getValue('department');
var inGroup = false;
var groupUser = new GlideRecord('sys_user_grmember');
groupUser.addQuery('group', 'sysid1'); // sysid group1
groupUser.addQuery('group', 'sysid2'); // sysid group2
groupUser.query();
while (groupUser.next()) {
if (groupUser.user == userID) {
inGroup = true;
break;
}
}
// Check if user is NOT in group OR department is NOT dept1 or dept2
if (!inGroup && (dept !== 'dept1' && dept !== 'dept2)) { // sysid dept1, dept2
return 'true';
}
}
return 'false';
}
});^^ For some reason the if condition works when && is used instead of ||
CLIENT SCRIPT
function onLoad() {
// get current user's sys_id
var sysid = g_user.userID;
// call Script Include via GlideAjax
var ga = new GlideAjax('checkUserGroupDept'); // Script Include name
ga.addParam('sysparm_name', 'getGroupDept'); // Function name in Script Include
ga.addParam('sysparm_sysid', sysid); // pass user sys_id
ga.getXMLAnswer(parseResponse);
function parseResponse(answer) {
if (answer == 'true') {
g_form.removeOption('service_request', 'value_option1', 'Name Option1');
g_form.removeOption('service_request', 'value_option2', 'Name Option2');
g_form.removeOption('service_request', 'value_option3', 'Name Option3');
}
}
}