How to check user has any roles from any group?.

Sai25
Giga Guru

Hi,

After group approval it goes for the license team approval. if user did not have same roles.

example :adela.cervantsz  has role scrum_user and when we are adding any group need to check user has roles not only one role any roles need to check from any group.

If found same roles need to bypass it to next activity and if did not found need to go for license team approval. So, license team will add the roles.

Below script i used to check the same, but not working.

answer = ifScript();
function ifScript() {
var x=current.variables.select_name;
x=gs.getUserID();
var y=current.variables.select_group;

var ge =new GlideRecord('sys_user_has_role');
ge.addQuery('user',x);
ge.query();
while(ge.next())
{
var gr=new GlideRecord('sys_group_has_role');
gr.addQuery('group',y);
gr.addQuery('role',ge.sys_id);
gr.query();
if(gr.next()) {
return 'yes';
}
}
return 'no';
}

below is the workflow.

find_real_file.png

Could you please help me.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Sai,

Can you explain with an example?

Is the script not working as expected?

Are you saying the select_name variable has some user

if that user has many roles say A,B,C

and now you want to check if the group selected in select_group variable has any of those role then return yes else return no

why you are using gs.getUserID() if you want to check role for select_name user?

update code as below

also ge.role will give you role sys_id while comparing in sys_group_has_role

answer = ifScript();
function ifScript() {
var x = current.variables.select_name; // I assume select_name is a reference variable

// x=gs.getUserID();

var y=current.variables.select_group;

var ge =new GlideRecord('sys_user_has_role');
ge.addQuery('user',x);
ge.query();
while(ge.next())
{
var gr=new GlideRecord('sys_group_has_role');
gr.addQuery('group',y);
gr.addQuery('role',ge.role);
gr.query();
if(gr.next()) {
return 'yes';
}
}
return 'no';
}

Regards

Ankur

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

View solution in original post

6 REPLIES 6

how do i make sure the user has two roles exactly?

Would appreciate if you could help on this one.

for exact 2 roles you can query sys_user_has_role table with the logged in user and get the row count from query

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