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

Hello Ridhima,

I noticed that you have marked my comment as correct earlier, but now it is not. I have also provided a rejoinder with the corrected line of code.

Posting it here again for your reference.

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.getValue("user"));
}

Kindly check and mark it correct so that the question is moved to the solved list.

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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Just to inform the answer you marked correct will only push 1 user to the answer array

you need to use toString() to get all the users pushed into that array.

OR

use answer.push(userRole.getValue('user'));

I have shared the complete script with both the changes.

Please mark appropriate response as correct & helpful.

Regards
Ankur

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Ridhima 

Let me know if I have answered your question.

If so, please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.

Regards
Ankur

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

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Did my answer help to resolve your question? Or do you still need some assistance on this? Let me know.

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