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

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
Kilo Patron

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>}

 

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

View solution in original post

2 REPLIES 2

Rafael Batistot
Kilo Patron

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>}

 

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

@Rafael Batistot thanks! for the solution