
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-17-2021 03:11 AM
Hi All,
I was given a task to create a new catalog item and one of the requirements is that some variable fields should be only visible to a certain group members. I'm trying to create a script in the Catalog UI Policy to do this, what I need to do is to get servicenow read if the current logged in user is a member of a certain group and if he/she is then some variable fields should be visible to them. Can anyone help as to how I can achieve this? I'm still new to this so I'm not really sure how this should work.
Thank you!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-18-2021 11:10 PM
Okay this is what it should have returned to you.
Now can you make sure you have given the correct variable names in your script. It should be the backend Name of the variable.
Can you share screenshot of your variable form which shows the name to make sure that has been entered is correct.
Like for example in my case, my current logged in user is part of two groups as shown below:
Now in my script I am checking if User is part of "Team Development Code Reviewers" queue then I am hiding my Variables 1 and Variables 2
This hides both my Variables on the form. For reference I have attached my Variable form screenshot as well below to make sure the name added to your script is the name highlighted:
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-18-2021 09:09 AM
Hi,
Please follow below steps to achieve your requirement:
Since you need to validate if current logged in User is a Member of certain Groups or not before the form is submitted the only way to handle this is doing a Server call and then based on the response returned from Server display the variables using Client Script:
1) First Create a Script Include and use the script below:
Make sure to check the "Client callable" checkbox as True
For example I have created a Script include named "validateMember" and use the script below:
Script Incldue:
var validateMember = Class.create();
validateMember.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateLoggedUserGroup: function() {
var arr = [];
var getLoggedInUser = this.getParameter('sysparm_user');
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user',getLoggedInUser);
gr.query();
while(gr.next()){
arr.push(gr.group.getDisplayValue().toString());
}
return arr.toString();
},
type: 'validateMember'
});
Now you need to write an On Load Catalog Client script on your catalog item and use the script below:
Script:
function onLoad() {
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('validateMember');
ga.addParam('sysparm_name', 'validateLoggedUserGroup');
ga.addParam('sysparm_user', g_user.userID);
ga.getXML(checkGroups);
function checkGroups(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var variablesToHide = ['variable1', 'variable2']; // Enter your variable backend Name which you want to hide
var groupsToValidate = 'Enter your Group Name which you want to check';
if (answer.indexOf(groupsToValidate) > -1) { //Check If USer if Member of Group Or Not which you want
for (var i = 0; i < variablesToHide.length; i++) {
g_form.setDisplay(variablesToHide[i], false);
}
}
}
}
In the Catalog Client Script, you just need to enter your Variables Name where mentioned in code above which you want to Hide and the Group Name which you want to check ijf logged in user is a part of that group or not and rest nothing need to be changed.
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-18-2021 09:10 AM
I have shared the complete End to End code for reference above.
This works in my PDI pretty well. Please let me know in case you are stuck anywhere in this code.
Regards,
Shloke
Regards,
Shloke

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-18-2021 07:54 PM
Hi
Thank you for sharing the end to end code, this is a great help for me to study as well later on, however I tried your script both in my PDI and our dev instance and it's not giving me any error in the backend nor in the service portal but it's also not doing anything, like the variables are still there and is not doing anything even if I impersonate a user that is a part of the group or not.
Please see below screenshot:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-18-2021 08:19 PM
Script which you have written is okay. Put an alert after line number 10 in your catalog client script and then reload your catalog form where you want to test this functionality Can you share the screenshot of what is coming in your alert here? Tested this again in my PDI now and works absolutely fine.
Regards,
Shloke
Regards,
Shloke

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-18-2021 10:51 PM
seems like it's only getting the string answer?