Add condition to UI Policy to check if user who is logged in has role 'admin'

Kasia5
Tera Contributor

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?

6 REPLIES 6

Rahul Talreja
Mega Sage
Mega Sage

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.

Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

Utpal Dutta
Tera Guru

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

Harish KM
Kilo Patron
Kilo Patron

Hi @Kasia5 You can handle this  with client script

example:

    if(g_user.hasRoleExactly('admin')) // login user has admin role?
    {
        g_form.setReadonly('fieldname',false);// readonly false
    }
else{
    g_form.setReadonly('fieldname',true);//set field readonly
}
Regards
Harish

Mark Roethof
Tera Patron
Tera Patron

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

LinkedIn