
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2022 08:14 AM
Hi,
We have a select box variable in a catalog item which has 3 choices, A,B,C. If the logged in user is a member of Group X, then they can see all 3 choices, but if they are a member of any other group, they should only see A and B.
I tested the principle using the following role based on load Client Script, which works exactly as we need it:
function onLoad() {
var isAdmin = g_user.hasRole('admin');
if (!isAdmin){
//alert('Current user is not an admin');
g_form.removeOption('variable','C');
}
}
My question is how to turn this into a group based condition. I tried the following, but the gs object should not be used in client scripts
function onLoad() {
var currentUser = gs.getUser();
if (currentUser.isMemberOf('X')){
g_form.addOption('variable','C');
Any help gratefully received.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2022 05:07 PM
Hi Cirrus,
Create a Script Include and call it from Client Script.
Variable choices defined as below.
Script Include
var UserInfoClient = Class.create();
UserInfoClient.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isUserInGroup: function() {
return gs.getUser().isMemberOf('<name of group>').toString();
},
type: 'UserInfoClient'
});
Client Script
function onLoad() {
var ajax = new GlideAjax('UserInfoClient');
ajax.addParam('sysparm_name', 'isUserInGroup');
ajax.getXMLAnswer(function(answer) {
if (answer.length > 0 && answer == 'true') {
g_form.addOption('variable', 'C', 'C');
}
});
}
Execution result
case 1:when user is a member of a group
case 2: when user is not a member of a group

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2022 05:52 AM
I'm creating everything in Global for this example.
It's working in Service Portal without any problem. Need to see each screenshot to find the problem.
Changed field name to "what" with choices "A" and "B".
Client script
Open in Service Portal

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2022 06:14 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2022 06:14 AM
Also, appreciate your help with this, thankyou

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2022 07:32 AM
Running your background script from your previous reply is failing:
Evaluator: com.glide.script.RhinoEcmaError: "UserInfoClient" is not defined. script : Line(1) column(0) ==> 1: var userInfo = new UserInfoClient(); 2: var isInGroup = userInfo.isUserInGroup(); 3: gs.info(isInGroup);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2022 07:44 AM
(As per my comment above) - Have you changed the Name of the client script to match your class name?