Script to combine approvers into a single array

Dan R
Kilo Expert

Good Afternoon SNC,

 

I'm creating an "approval process" where reminder emails get sent out on a weekly basis, and if no approval is obtained after a month, the request gets cancelled and an email notification is sent to the requester advising them of the cancellation, and who the approver(s) of the ticket were so they can reach out to the approver(s) to discuss the request.

This works just fine for a catalog item that has a single approver, however if there are multiple approvers on a RITM, then the requester will end up with multiple "Cancellation notification" emails, 1-per approver on the request.

 

My goal here is to create an array in an email script so I can parse the sysapproval_approver table, and when the query finds 2 records with the same sysapproval field, it takes the value in the approver field on each ticket and creates an array. From there, the array would be printed using the email script to the notification... With this way, if there is 1 approver or if there's 10, the array will output the list of approvers in the email and send just 1 email with all the approvers.

 

My javascript is a little rusty and I'm struggling to get this coded out. Any ideas or can anyone take a stab at this and share their results? I don't think this is too crazy of an ask. Let me know what you think.

 

Thanks in advance!

 

Dan R

 

 

 

 

1 ACCEPTED SOLUTION

I was able to resolve the issue by making a substitution in your formula. the working script for me is: 

 

var currentApprovalFor = current.sysapproval; // This is the line I added to assign the sysid of the "Approval for" field on the sysapproval_approver record to a variable


var appList=[]; // The array which will hold the approver list
var app = new GlideRecord('sysapproval_approver');
app.addQuery('sysapproval',currentApprovalFor); // I updated the 2nd parameter of this addQuery to look for all tickets with the sysapproval field = the current sysapproval_approver record
app.addQuery('state','requested');

app.query();

while (app.next())
{
appList.push(app.approver.getDisplayValue());
}
template.print('<font size="2" color="#808080" face="helvetica"><b>Approver(s):</b></font>');
template.print(appList);

View solution in original post

7 REPLIES 7

Yes. You can add it as an email script

 

var appList=[]; // The array which will hold the approver list
var app = new GlideRecord('sysapproval_approver');
app.addQuery('sysapproval',current.getValue('sys_id'));
app.addQuery('state','requested');

app.query();

while (app.next())
{
appList.push(app.approver.getDisplayValue());
}
template.print(ritm,appList);

Please mark this response as correct or helpful if it assisted you with your question.

hmmm, this doesn't seem to work for me when I try. Am I missing something?

 

I have the below script in an email script that I am exposing on an email notification.

When trying I'm getting :

find_real_file.png

 

var appList=[]; // The array which will hold the approver list
var app = new GlideRecord('sysapproval_approver');
app.addQuery('sysapproval',current.getValue('sys_id'));
app.addQuery('state','requested');

app.query();

while (app.next())
{
appList.push(app.approver.getDisplayValue());
}

template.print('<font size="2" color="#808080" face="helvetica"><b>Approver(s):</b></font>');
template.print(appList);

I was able to resolve the issue by making a substitution in your formula. the working script for me is: 

 

var currentApprovalFor = current.sysapproval; // This is the line I added to assign the sysid of the "Approval for" field on the sysapproval_approver record to a variable


var appList=[]; // The array which will hold the approver list
var app = new GlideRecord('sysapproval_approver');
app.addQuery('sysapproval',currentApprovalFor); // I updated the 2nd parameter of this addQuery to look for all tickets with the sysapproval field = the current sysapproval_approver record
app.addQuery('state','requested');

app.query();

while (app.next())
{
appList.push(app.approver.getDisplayValue());
}
template.print('<font size="2" color="#808080" face="helvetica"><b>Approver(s):</b></font>');
template.print(appList);