Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Hide select box option based on role of the requested for user entered on catalog item.

Aprille
Mega Guru

Hi everyone.  I'm stuck.  I need to remove an option of a select box if the requested for user does not have the 'itil' role.  I've looked through the community and found several scripts for getting the role from the g_user, but the requested for in this case is not always the system user.  I've variations on this script and can't seem to get it to work:

function onLoad() {
	//who to display it for - admin
	var isAdmin = g_user.hasRole('admin');
	
	//what field to show/hide from - contact_type
	var contactType = g_form.getValue('contact_type');
	
	//hide value for non values
	if (!isAdmin && (contactType == 'integration')){
		g_form.removeOption('contact_type', 'integration');
	}
}

The above script is great if the requested for happens to be the logged in user, but they aren't always the same.

The only thing I saw used a script include with a catalog client script here: How to check the requested for has ITIL role or not, however I need to hide a specific option in the select box "environment" variable if the requested for user does not have the itil role.  I'm probably overthinking this and I need help.  Any help with a script would be very much appreciated!  Thank you!

 

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can use onLoad client script + GlideAjax and send the requested_for field value and check if user has that role or not

Based on that remove the option

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Al-jhon
Kilo Sage

Hello @Aprille did you meet your requirements already? If yes, how?

Al-jhon
Kilo Sage

@Aprille , in your catalog client script you can gliderecord on sys_user_has_role table.
and get the sysid of requested_for from RITM

 

var rfor = g_form.getValue('request.requested_for');

var contactType = g_form.getValue('contact_type');

var checkrole = new GlideRecord('sys_user_has_role')
checkrole.addQuery('user.sys_id','=', rfor); 
checkrole.addQuery('role.name','DOES NOT CONTAIN', "itil"); //user that has no itil role
checkrole.query();

while(checkrole.next()){

g_form.removeOption('contact_type', 'integration');

}

try this idea. 🙂