How to detect if user has a certain role on the Catalog Item?

mattmg
Tera Guru

Hey,

 

I have a requested by field on the form (refferenced to sys_user table), which based on it's default value detect user (javascript:gs.getUserID()) - that works fine. 

 

Now I would like to create another field

'Does user has a 'itil' role?', how on the Catalog Item/Record producer can query for this and push the outcome to that new field?

 

TIA

Matt

1 ACCEPTED SOLUTION

@mattmg 

try to use onLoad catalog client script

Ensure you perform testing by logging in and not impersonating

function onLoad() {
    if (g_user.hasRole('itil'))
        g_form.setValue('variable', 'Yes');
    else
        g_form.setValue('variable', 'No');
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

11 REPLIES 11

Najmuddin Mohd
Mega Sage

Hello @mattmg ,

You can use an onchange catalog client script on the requested by,

 

g_user.hasRole(g_form.getValue(newValue))

It returns true if the user has ITIL role.

You can write an if else condition and populate the value in the Second field, 'Does user has a 'itil' role.


2nd Approach, in the default value, you can write similar to gs.getUserID(), you can write,

javascript:gs.hasRole('itil'). It directly returns True or False in the Second field.



If the above information helps you, Kindly mark it as Helpful and Accept the solution.
Regards,
Najmuddin.

Tai Vu
Kilo Patron
Kilo Patron

Hi @mattmg 

If your variable is in Yes/No type. You can try to put this line below into as Default Value.

javascript:gs.hasRole("itil") ? "Yes" : "No"

TaiVu_0-1739428651933.png

 

Cheers,

Tai Vu

mattmg_0-1739447113310.png

 

this is what I get.

 

When I do 'javascript:gs.hasRole("itil") ? "Yes" : "No"'

 

it comes back Yes,

 

but equally if I change the role, that me as a logged in user doesn't have, like:

 

javascript:gs.hasRole("test_role_that _doesnt_exsist") ? "Yes" : "No"

 

it comes back with Yes

@mattmg 

try to use onLoad catalog client script

Ensure you perform testing by logging in and not impersonating

function onLoad() {
    if (g_user.hasRole('itil'))
        g_form.setValue('variable', 'Yes');
    else
        g_form.setValue('variable', 'No');
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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