I have to trigger a notification on demand table when the Approval is Rejected

Trupti Krishnam
Tera Contributor

I have to trigger a notification on demand table when the Approval is Rejected . I configured a notification to achieve this but in the notification i have to dynamically show the comments that was added during the rejection

this comments field is from sysapproval table . due to which i cannot get it fields directly in the field section of the notification . I think i have to write a mail script to get the comments of all the Approvals . Please help me with this!

1 ACCEPTED SOLUTION

Rafael Batistot
Tera Sage

Hi @Trupti Krishnam 

 


You’re right, mail scripts is the right track. See an example of script 

 

(function runMailScript(current, template, email, email_action, event) {
var comments = [];

// Query sysapproval_approver table for this approval record
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('sysapproval', current.sys_id); // link to your RITM/Request/Change
gr.addQuery('state', 'rejected'); // only rejected approvals
gr.orderByDesc('sys_updated_on'); // latest first
gr.query();

while (gr.next()) {
comments.push(gr.comments.toString()); // collect rejection comments
}

if (comments.length > 0) {
template.print('<b>Rejection Comments:</b><br/>');
for (var i = 0; i < comments.length; i++) {
template.print('- ' + comments[i] + '<br/>');
}
} else {
template.print('No rejection comments available.');
}
})(current, template, email, email_action, event);

 

In your Notification → under the Message HTML/body, insert:

 

${mail_script:<name_of_mail_script>}

 

View solution in original post

2 REPLIES 2

Rafael Batistot
Tera Sage

Hi @Trupti Krishnam 

 


You’re right, mail scripts is the right track. See an example of script 

 

(function runMailScript(current, template, email, email_action, event) {
var comments = [];

// Query sysapproval_approver table for this approval record
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('sysapproval', current.sys_id); // link to your RITM/Request/Change
gr.addQuery('state', 'rejected'); // only rejected approvals
gr.orderByDesc('sys_updated_on'); // latest first
gr.query();

while (gr.next()) {
comments.push(gr.comments.toString()); // collect rejection comments
}

if (comments.length > 0) {
template.print('<b>Rejection Comments:</b><br/>');
for (var i = 0; i < comments.length; i++) {
template.print('- ' + comments[i] + '<br/>');
}
} else {
template.print('No rejection comments available.');
}
})(current, template, email, email_action, event);

 

In your Notification → under the Message HTML/body, insert:

 

${mail_script:<name_of_mail_script>}