Add condition to UI Policy to check if user who is logged in has role 'admin'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2024 12:04 AM
Hi All!
I have a Catalog UI Policy which make one field in INC form read only.
I want to add condition to check if currently logged in user has role 'admin' to exclude this users to make them able to change this field if it will be needed.
So condition will sth like: if user has not role admin then make field read only.
How can I achieve this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2024 12:08 AM
Hi @Kasia5 ,
you can achieve this by utilizing the `g_user` object which can be used to retrieve information about the currently logged in user. You can use its `hasRole()` method to check if the user has the 'admin' role.
Here's a sample code for your condition:
if (!g_user.hasRole('admin')) {
// code to make the field read only
}
You need to put this code in the 'Script' field of the Catalog UI Policy. This script will run on the client side, and the field will be made read only if the user is not an admin.
Thanks and Regards,
Rahul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2024 12:08 AM
Hi Kasia,
You can use g_user.hasRole('admin') to check it. If it returns true mean user is an admin.
If my answer helps then please mark it correct.
Thanks,
Utpal

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2024 12:10 AM
Hi @Kasia5 You can handle this with client script
example:
Harish

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2024 12:31 AM
Hi there,
I'm confused, is this about Catalog or not? Because you do mention Catalog UI Policy, though you also mention field in INC form. That sounds odd.
Do you mean UI Policy instead? If so, would using Acess Controls be a more reliable, safer, and better performing option for you?
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field