Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Hide a field in a catalog item view based on requestor's user role

GamNOW
Tera Contributor

Hello Everyone,

 

How can you make a certain field invisible and not mandatory?

 

There is a UI policy set to where if urgent request is 'Yes'

Need by Date and Approving Deputy Director is mandatory and visible.

 

But in a catalog item view in the Service Portal, I want to make 'Approving Deputy Director' invisible and not mandatory if the person submitting the form has 'deputy_director' role.  How can I achieve this?  (I am using Yokohama)

GamNOW_0-1760134289242.png

 

10 REPLIES 10

Radim Ceresnak
Tera Contributor

Hello GamNOW,

you have at least two options using scripting:
- either a Catalog Client Script with Type = onLoad; checkbox Applies on a Catalog Item view checked and a script similar to this one

function onLoad() {
	if (g_user.hasRole('deputy_director')) {
		// hide field _field_name_ (to show, replace false with true)
		g_form.setDisplay('_field_name_', false);
		// disable mandatory for field _field_name_ (to set mandatory, replace false with true)
		g_form.setMandatory('_field_name_', false);
	}
}


 - or a Catalog UI Policy without any condition, but with checkbox Applies on a Catalog Item view checked, OnLoad checked too, on tab Scripts checked Run Scripts and a script similar to this one 

function onCondition() {
	if (g_user.hasRole('deputy_director')) {
		// hide field _field_name_ (to show, replace false with true)
		g_form.setDisplay('_field_name_', false);
		// disable mandatory for field _field_name_ (to set mandatory, replace false with true)
		g_form.setMandatory('_field_name_', false);
	}
}


Please mark Correct / Helpful if applicable

Regards,
Radim

NityaB161013953
Tera Patron

Hi @GamNOW,

In Service Portal (Yokohama), UI Policies don’t always control visibility as expected. To make “Approving Deputy Director” invisible and not mandatory for users with the deputy_director role:

✅ Best approach:
Use a Catalog Client Script (onLoad) —

function onLoad() {
    if (g_user.hasRole('deputy_director')) {
        g_form.setDisplay('approving_deputy_director', false);
        g_form.setMandatory('approving_deputy_director', false);
    }
}

Alternate:
Add a Condition Script in the UI Policy to skip enforcement for users with that role:

!g_user.hasRole('deputy_director')

In short:
Use a Catalog Client Script for reliable behavior in Service Portal; UI Policy alone won’t work consistently.

Ankur Bawiskar
Tera Patron

@GamNOW 

UI policy won't help here as you need to check role

You will require client script

what did you start with and where are you stuck?

 

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

I have made the catalog client script as described from this post.  It is not performing the expected behavior when impersonating a user with deputy_director role.

 

GamNOW_0-1760372959511.png

 

 

 

 

 

GamNOW_1-1760372959515.png

 

Hi Ankur,

 

 

I do have an existing Catalog UI policy that calls this same variable in UI Policy action, but regardless, I need a user role check that if the user has the role, set the display false, and not mandatory.

Hello GamNOW,

 

try one trick  - swap the commands - ie setMandatory first and setDisplay second

function onLoad() {
	if (g_user.hasRole('deputy_director')) {
		// disable mandatory for field _field_name_ (to set mandatory, replace false with true)
		g_form.setMandatory('_field_name_', false);
		// hide field _field_name_ (to show, replace false with true)
		g_form.setDisplay('_field_name_', false);
	}
}