How to set approval for users with specific role in workflow - Approval user activity script ?

Ridhima4
Kilo Contributor

Hi, 

I want the users with specific role to approve a request, My workflow is working fine but the Approval user script is not working, Please help me with the same. Below is my script mentioned:

var answer = [];
var userRole = new GlideRecord('sys_user_has_role');
userRole.addQuery('role', 'x_adsr_recruit.hiring_manager');
userRole.query();
while(userRole.next()){
answer.push(userRole.user.sys_id);
}

Thanks 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

updated script should work fine now

please use this

see the changes in bold

1) use role.name to query with role name

2) use toString() while pushing user to answer array

var answer = [];
var userRole = new GlideRecord('sys_user_has_role');
userRole.addQuery('role.name', 'x_adsr_recruit.hiring_manager');
userRole.query();
while(userRole.next()){
answer.push(userRole.user.toString());
}

Regards
Ankur

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

View solution in original post

9 REPLIES 9

Mark Roethof
Tera Patron
Tera Patron

Hi there,

The role field on sys_user_has_role, is a reference. So a sys_id. You are quering now on text. Just update that in your query and your fine, for example using role.name to query on or the sys_id instead of the role name.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Mark Roethof
Tera Patron
Tera Patron

find_real_file.png

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

asifnoor
Kilo Patron

Try like this.

var answer = [];
var userRole = new GlideRecord('sys_user_has_role');
userRole.addQuery('role.name', 'x_adsr_recruit.hiring_manager');
userRole.query();
while(userRole.next()){
  answer.push(userRole.user);
}

Mark the comment as a correct answer if this has solved the problem.

hi,

What Ankur mentioned is correct. If there are multiple users, just change this 1 line in my code.

 answer.push(userRole.user);

TO

 answer.push(userRole.getValue("user"));