Script in workflow to send to the correct approver based on variable selection

lisaO
Giga Contributor

Hi,

Sorry, I am new to scripting...

I have a selectbox field in my form which contains only names of individuals for the users to select. Whoever they select will be the approver whereby in the workflow, it will send to the correct person based on selection. This selectbox called approver does not tie to any user table and is just values containing names.

In my Approval workflow, I want to retrieve the values from the selectbox and from there, i will tie to user table so that it will call the actual person as the Approver. I am not sure if the below script I placed in "Additional approvers script" in my Approval workflow is correct:

Can anyone help please? Thank you

// Set the variable 'answer' to a comma-separated list of user ids and/or group ids or an array of user/group ids to add as approvers.
//

answer = [];
var getUser = new GlideRecord('sys_user');
// gs.log("u_approver =" +u_approver);

if(current.variables.approver == 'head_onestop_sac')
{
getUser.addQuery('title', 'Head, One Stop @ SAC');
}
else if(current.variables.approver == 'director_admissions')
{
getUser.addQuery('title', 'Director');
getUser.addQuery('department', 'Office of Admissions');
}
answer.push(u_approver);
// answer.push('id2');

1 ACCEPTED SOLUTION

AbhishekGardade
Giga Sage

Hello Lisa,

Your code should be like this:

// Set the variable 'answer' to a comma-separated list of user ids and/or group ids or an array of user/group ids to add as approvers.
answer = [];

var getUser = new GlideRecord('sys_user');

if(current.variables.approver == 'head_onestop_sac')
{
getUser.addQuery('title', 'Head, One Stop @ SAC');
getUser.query();
while(getUser.next()){
gs.log(" First IF :user name:"+getUser.name);
answer.push(getUser.sys_id);
}
}
else if(current.variables.approver == 'director_admissions')
{
getUser.addQuery('title', 'Director');
getUser.addQuery('department', 'Office of Admissions');
getUser.query();
while(getUser.next()){
gs.log("ELSE IF:user name:"+getUser.name);
answer.push(getUser.sys_id);
}
}

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

Thank you,
Abhishek Gardade

View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Lisa,

Do you have full name of the user in the variable because based on that query will take place?

what is u_approver? is that the name of the variable which holds the full name of the user

also you are just querying based on department or title; what about the user which is available in the selectbox field

 

why you are having query of title and department if you want to send approval to that user directly?

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

AbhishekGardade
Giga Sage

What is u_approver here? Where it is defined

Thanks,

Abhishek

Thank you,
Abhishek Gardade

AbhishekGardade
Giga Sage

Hello Lisa,

Your code should be like this:

// Set the variable 'answer' to a comma-separated list of user ids and/or group ids or an array of user/group ids to add as approvers.
answer = [];

var getUser = new GlideRecord('sys_user');

if(current.variables.approver == 'head_onestop_sac')
{
getUser.addQuery('title', 'Head, One Stop @ SAC');
getUser.query();
while(getUser.next()){
gs.log(" First IF :user name:"+getUser.name);
answer.push(getUser.sys_id);
}
}
else if(current.variables.approver == 'director_admissions')
{
getUser.addQuery('title', 'Director');
getUser.addQuery('department', 'Office of Admissions');
getUser.query();
while(getUser.next()){
gs.log("ELSE IF:user name:"+getUser.name);
answer.push(getUser.sys_id);
}
}

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

Thank you,
Abhishek Gardade

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,


What is the connection or common factory between this variable and User table field


Thanks,

Ashutosh