Check box variable in the Catalog item visible based on the Requested for variable's User ID
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2024 10:19 PM
Hi Everyone,
I have a requirement for a new catalog item. I have a variable set, in that there is a variable "Requested for," and it is a reference variable that is fetching the user table. So the condition is that we have a check box variable called "Prod." This check box should only be available if the requested user ID starts with ''ex''. If the user ID does not start with an ex'', we don't need this option.
I am attaching the screen shots as well:
This is the variable set, and based on the variable "Requested for", if the user's user Id Starts with 'ex'
Then tha check box variable should be visible for other user ID it should not be visible.
I tried with the catalog client script, ui policy as well but it is not working, I am not able to get the condition where it is fetching "user ID" of the user.
Could you please help me to achieve the requirement.
Thank you.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2024 10:40 PM - edited 01-18-2024 10:40 PM
Hi @Tuhina Sharma You can create a script include with below function
getRequestorDetails:function()
{
var SysID = this.getParameter('sysparm_id');//reqfor sysid
var user = new GlideRecord('sys_user');
user.addQuery('sys_id',SysID);
user.addEncodedQuery(user_nameSTARTSWITHex); //encodedquery
user.query();
if(user.next())
{
return true; //return true if record found
}
return false; // return false if no record
},
then have a client script to show variable
var si = new GlideAjax('Script Include Name');
si.addParam('sysparm_name', 'getRequestorDetails');// scriptinclude function name
si.addParam('sysparm_sysid', newValue); // opened for value
si.getXMLAnswer(parseResponse);
function parseResponse(answer) {
alert(answer);
if(answer == 'true')
{
g_form.setVisible('variableName',true);
}
else
{
g_form.setVisible('variableName',false);
}
Harish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2024 11:10 PM
Hello @Tuhina Sharma
You can user script include and onChange client script to achieve this :
1. Script Include
Name : userIDUtils
var userIDUtils = Class.create();
userIDUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserId: function() {
var userId;
/*1. Get User sys_id from Catalog item client script */
var user = this.getParameter('sysparm_user');
/*2. glide record on user table and get UserId */
var grUser = new GlideRecord('sys_user');
grUser.addQuery('sys_id', user);
grUser.query();
if (grUser.next()) {
userId = grUser.getValue('user_name');
}
return userId;
},
type: 'userIDUtils'
});
2. onChange client script on 'Requested for' variable
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
/* 1. Get Requested for value */
var user = g_form.getValue('requested_for');
/* 2. Call glideAjax */
var gaUser = new GlideAjax('userIDUtils'); // Glide ajax
gaUser.addParam('sysparm_name','getUserId'); // function name
gaUser.addParam('sysparm_user',user);
gaUser.getXMLAnswer(callBackFunction);
function callBackFunction(answer){
var userId = answer;
if(userId.startsWith('ex')){
//make variable hidden
} else {
//make variable visible
}
}
}
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates