Approval script using a reference variable

kmusselle
Kilo Explorer

Hi,

I have a service catalog item called Travel Request Form:

1. There is a variable field that is a reference field to a Table called 'u_budget_holders_and_pas'

2. This table has two fields 'u_budget_holder' and 'u_pa_email'.

I have the script below that should send the approval to the names that are selected in the Table but the approvals are going to the first line entry in the table rather than the one i select, any ideas how to get the approval to send to the name i select and not the first entry, there could be up to 10 people to choose from?

// 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.
//
// For example:

var bHolder = current.variables.budget_holder;
var paHolder;


var holderPA = new GlideRecord('u_budget_holders_and_pas');
holderPA.addQuery('user',bHolder);
holderPA.query();
if (holderPA.next()){
  paHolder = holderPA.u_pa_email;

var holderBH = new GlideRecord('u_budget_holders_and_pas');
holderBH.addQuery('user',paHolder);
holderBH.query();
if (holderBH.next()){
  bHolder = holderBH.u_budget_holder;

}
}

answer = [];
answer.push(bHolder);
answer.push(paHolder);
//return answer;

thanks

kathryn

1 ACCEPTED SOLUTION

Hi Kathryn,



If I understand this correctly, your variable is a reference to a custom table you've built with 2 user fields on it, and you want to send an approval to each of the 2 users? If that's the case you can do this a lot simpler just by dot-walking the reference variable like this:



answer = [];


answer.push(current.variables.budget_holder.u_pa_email);


answer.push(current.variables.budget_holder.u_budget_holder);



Please let me know if I've misunderstood your requirements.


View solution in original post

5 REPLIES 5

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Kathryn,



When you're querying your custom table, I don't think the following line would work since there would be no user field on the table:


holderPA.addQuery('user',bHolder);




It should probably read:


holderPA.addQuery('u_budget_holder',bHolder);




This will only work if your variable is a reference to the same table as the u_budget_holder field.


Hi Brad,



I have updated my code to the below but it is still sending the approval to the first line of users in the budget holder table?



My Variable is a reference to the table and the field that is selected is the budget holder field.



// 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.
//
// For example:



var bHolder = current.variables.budget_holder;
var paHolder;


var holderPA = new GlideRecord('u_budget_holders_and_pas');
holderPA.addQuery('u_pa_email',paHolder);
holderPA.query();
if (holderPA.next()){
  paHolder = holderPA.u_pa_email;


var holderBH = new GlideRecord('u_budget_holders_and_pas');
holderPA.addQuery('u_budget_holder',bHolder);
holderBH.query();
if (holderBH.next()){
  bHolder = holderBH.u_budget_holder;
}
}


answer = [];
answer.push(bHolder);
answer.push(paHolder);


//return answer;




Hi Kathryn,



If I understand this correctly, your variable is a reference to a custom table you've built with 2 user fields on it, and you want to send an approval to each of the 2 users? If that's the case you can do this a lot simpler just by dot-walking the reference variable like this:



answer = [];


answer.push(current.variables.budget_holder.u_pa_email);


answer.push(current.variables.budget_holder.u_budget_holder);



Please let me know if I've misunderstood your requirements.


thank you!