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

lisaO
Giga Contributor

Sorry for not being clear enough.

My variable name "approver" is a select box with all the choices that contains labels and names. They are purely texts without relating to the actual person. (screenshot below). There is another client script to display only the choices that appear in the select box based on certain conditions. Once requester has chosen the person from the dropdown, the approval is supposed to route to that person. All this tagging to the actual person, I am doing it under the Approval - User workflow > Advanced script.

What i am stuck is the script to point to the actual person. So for example based on screenshot, Head, OneStop@SAC is the title name of a person. How do i point in a way that, if requester select Head,OneStop@SAC, Mr XYZ in their form, in the approval workflow, it is routing to this person. I just need to call out this person in sys id table.

Thank you

find_real_file.png

Hello Lisa,

have you try checking my script mentioned above? its similar to your requirement.

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

Thank you,
Abhishek Gardade

Hi Abhishek,

Thank you so much! It works exactly as what I wanted!

Varsha Jadhav1
Giga Expert

Hi Lisa,

The field mu_approver is a reference field .   Top left of the image is the workflow , middle right of the image is the workings inside the approval user object (in the workflow) and bottom left of the image is the catalog item with the variable that is used in the workflow.  

find below attachment

 

Varsha Jadhav1
Giga Expert

Hi Lisa,

Go through below link

 

https://community.servicenow.com/community?id=community_question&sys_id=55888321db5cdbc01dcaf3231f96...

 

if your query has resolved please mark the answer correct or helpful