The CreatorCon Call for Content is officially open! Get started here.

Scheduled Job to Send List of Approvals to Each Unique Approver

triciav
Kilo Sage

I have a scheduled job with a Script Include that gets only the Unique Approvers. So if there are 4 approvals pending for an approver I am returning their email just once.

This works fine.

The problem I have is I need to send each individual their own email notification with a List of their Pending Approvals.
Example:

"Abe" has 4 approvals pending

Send Abe 1 email with a Table that Lists all 4 of his Approvals
"Jane" has 3 approvals pending

Send Jane 1 email with a table that lists all 3 of her approvals.

 

etc...

My Scheduled Job is Calling the script include and has a script to create a table to send in the email.

How can I get this to email each individual approver with their own list of approvals?

var myScriptInclude = new GRCNotificationsUtils().getAllRamApprovers();
My Scheduled Job Script Contains the following script to build the table.
var title = 'Pending Approvals';

var q = 'active=true^state=1';
var page_header = '<p><h1>' + title + '</h1></p><p>';
var table_header = '<table cellpadding=10 border=1>' +
'<tr>' +
'<td>Number</td>' +
'<td>Short Description</td>' +
'<td>Created on</td>' +
'</tr>';
var page_footer = '</table>';
var msg_body = '';

if (gs.nil(recipients)) {
var gri = new GlideRecord('sn_risk_advanced_risk_assessment_instance');

gri.addEncodedQuery(q);
gri.query();
while (gri.next()) {

// recipients.push(gri.assigned_to.email).join(', ');
gs.info("TRICIA Recipients " + recipients);
msg_body = msg_body +
'<tr><td><a href="' + gri.getLink() + '">' + gri.getDisplayValue() + '</a></td>' +
'<td>' + gri.short_description + '</td>' +
'<td>' + gri.caller_id.getDisplayValue() + '</td>' +
'<td>' + gri.caller_id.title.getDisplayValue() + '</td>' +
'<td>' + gri.caller_id.department.getDisplayValue() + '</td>' +
'<td>' + gri.assigned_to.getDisplayValue() + '</td>' +
'<td>' + gri.priority + '</td>' +
'<td>' + gri.category.getDisplayValue() + '</td>' +
'<td>' + gri.sys_created_on + '</td>' +

'</tr>';
}
//<font color="red">This is some text!</font>
msg_body = page_header + table_header + msg_body + page_footer;
//gs.log("reported changes: " + msg_body);
var gr_email = new GlideRecord('sys_email');
gr_email.initialize();
gr_email.recipients = recipients;
gr_email.subject = title;
gr_email.body = msg_body;
gr_email.type = 'send-ready';
gr_email.insert();
}
Script Include:
getAllRamApprovers: function() {
var ramGr = new GlideAggregate('sn_risk_advanced_risk_assessment_instance');
ramGr.addEncodedQuery('state=6^risk_assessment_methodology=145a3700dbd95594d1939b6ed3961947');
ramGr.groupBy('approver_user');
ramGr.query();
var ramIds = [];

while (ramGr.next()) {
ramIds.push(ramGr.approver_user);
var arrayUtil = new global.ArrayUtil();
ramIds = arrayUtil.unique(ramIds);

for (var i = 0; i < ramIds.length; i++) {

var gu = new GlideAggregate('sys_user');
gu.addQuery('sys_id', ramIds[i]);
gu.query();
while (gu.next()) {
// gs.eventQueue('sn_risk_advanced.risk.assessment.approva', null, gu.email);
gs.info("TRICIA EMAILS "+gu.email);
}
return gu.email;
}
}
},
 
My Scheduled Job Script Contains the following script to build the table.
var title = 'Pending Approvals';

var q = 'active=true^state=1';
var page_header = '<p><h1>' + title + '</h1></p><p>';
var table_header = '<table cellpadding=10 border=1>' +
'<tr>' +
'<td>Number</td>' +
'<td>Short Description</td>' +
'<td>Created on</td>' +
'</tr>';
var page_footer = '</table>';
var msg_body = '';

if (gs.nil(recipients)) {
var gri = new GlideRecord('sn_risk_advanced_risk_assessment_instance');

gri.addEncodedQuery(q);
gri.query();
while (gri.next()) {

// recipients.push(gri.assigned_to.email).join(', ');
gs.info("TRICIA Recipients " + recipients);
msg_body = msg_body +
'<tr><td><a href="' + gri.getLink() + '">' + gri.getDisplayValue() + '</a></td>' +
'<td>' + gri.short_description + '</td>' +
'<td>' + gri.caller_id.getDisplayValue() + '</td>' +
'<td>' + gri.caller_id.title.getDisplayValue() + '</td>' +
'<td>' + gri.caller_id.department.getDisplayValue() + '</td>' +
'<td>' + gri.assigned_to.getDisplayValue() + '</td>' +
'<td>' + gri.priority + '</td>' +
'<td>' + gri.category.getDisplayValue() + '</td>' +
'<td>' + gri.sys_created_on + '</td>' +

'</tr>';
}
//<font color="red">This is some text!</font>
msg_body = page_header + table_header + msg_body + page_footer;
//gs.log("reported changes: " + msg_body);
var gr_email = new GlideRecord('sys_email');
gr_email.initialize();
gr_email.recipients = recipients;
gr_email.subject = title;
gr_email.body = msg_body;
gr_email.type = 'send-ready';
gr_email.insert();
}
2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@triciav 

while building the html table iterate over the records and include link to each approval record on each row

what's the challenge?

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

The challenge is I need to send the list of records for each individual approver that is returned from the script include.

So that each approver gets a seperate email with the list of their approvals only.