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

How do i thigh this up with the field?

 

mattmg_0-1739448759840.png

onChange can point out the field, but not onLoad

ok, I replaced variables with the field that needs to receive outcome, however, if I add madeup role, it still brings 'Yes'

 

mattmg_1-1739448989723.png

Can I do instead of g_user some kind of query to the referente field where I actually detected user?

@mattmg 

why are you testing with some random role which is not present in instance?

also I informed please test by actual user and not with admin

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

Hi @mattmg 

gs.hasRole also return Yes if you're Admin.

Flag that indicates whether the current user has at least one of the specified roles.

Possible values:
  • true: User has at least one of the passed-in roles. Also returns true if the user has the administrator role.
  • false: User does not have any of the passed-in roles.

so do you want to check that if the user has exact role? 

Cheers,

Tai Vu

@mattmg Oops sorry Matt. Can you clarify that is it to validate the user from the Requested by field? (Not the logged in user).