Hide a field in a catalog item view based on requestor's user role
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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);
}
}