- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2024 06:34 AM - edited 11-18-2024 06:35 AM
Hi Experts,
I am trying to sent previous approval comment to the next approver in RITM approval notification and has written below email notification script and calling it in approval notification using ${email_script:email_script_name}
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('document_id', current.sys_id);
gr.addQuery("state", "approved"); // Filter out only approved record if you want all comment this line
gr.query();
// If no approved records found
if (!gr.hasNext()) {
template.print('No approved records found.');
} else {
while (gr.next()) {
template.print('<b>Approver Details:</b><br>');
template.print('<b>Approver:</b> ' + gr.getDisplayValue("approver") + '<br>');
template.print('<b>Approval State:</b> ' + gr.getValue("state") + '<br>');
template.print('<b>Approved Time:</b> ' + gr.getDisplayValue("sys_updated_on") + '<br>');
template.print('<b>Comments:</b> ' + gr.comments.getJournalEntry(1) + '<br><br>');
}
}
})(current, template, email, email_action, event);
But it gives comment as "No Approved Records found" even if comments are present under previous approver. Please advice how can I achieve the requirement.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2024 02:13 AM - edited 11-19-2024 02:19 AM
Hi Runjay,
I modified the code as below and now its working fine as required. I tested it from fulfiller view only not sure how it will work for users approving it over email. I need to check that. Also I tried getting all approval comments instead of only latest comment.
Also instead of using current.sys_id. I used current.document_id
// Use the document_id of the current record to fetch previous approvals
var gr = new GlideRecord('sysapproval_approver');
// Query to find previous approval records attached to the same document_id
gr.addQuery('document_id', current.document_id); // Use the document_id of the current record
gr.addQuery("state", "approved");
gr.orderByDesc('sys_updated_on');
gs.info('Current document_id being queried: ' + current.document_id);
gr.query();
var previousApprovers = [];
while (gr.next()) {
previousApprovers.push(gr);
}
if (previousApprovers.length > 0) {
gs.info('Found ' + previousApprovers.length + ' previous approvals for document_id ' + current.document_id);
// Go through each previous approver
previousApprovers.forEach(function(approvalRecord) {
template.print('<b>Approver Details:</b><br>');
template.print('<b>Approver:</b> ' + approvalRecord.getDisplayValue("approver") + '<br>');
template.print('<b>Approval State:</b> ' + approvalRecord.getValue("state") + '<br>');
template.print('<b>Approved Time:</b> ' + approvalRecord.getDisplayValue("sys_updated_on") + '<br>');
// Get all journal entries related to the approval record's comments
var commentGr = new GlideRecord('sys_journal_field');
commentGr.addQuery('element_id', approvalRecord.sys_id);
commentGr.addQuery('element', 'comments');
commentGr.orderBy('sys_created_on');
commentGr.query();
//Go through all comments for this approval record
if (commentGr.hasNext()) {
template.print('<b>Comments:</b><br>');
while (commentGr.next()) {
template.print(commentGr.value + '<br>');
}
} else {
template.print('<b>No comments available.</b><br>');
}
template.print('<br>');
});
} else {
gs.info('No approved records found for document_id: ' + current.document_id);
template.print('No previous approved records found.');
}
Thank you for all the help. Appreciate it!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2024 06:26 PM
@Ankita Gupte The query for document_id is correct, that would be ok , pls try below updated script if works for you, if not then you should try through background script by just validating script value:
(function runMailScript(current, template, email, email_action, event) {
// Initialize GlideRecord for sysapproval_approver table
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('document_id', current.sys_id);
gr.addQuery('state', 'approved'); // Only consider records with 'approved' state
gr.orderByDesc('sys_updated_on'); // Sort by the most recent approval
gr.query();
// Check if there are any approved records
if (gr.next()) {
// Get the latest journal entry (most recent comment)
var latestComment = gr.comments.getJournalEntry(0);
if (latestComment) {
// Print the details of the previous approver and the latest comment
template.print('<b>Previous Approver Details:</b><br>');
template.print('<b>Approver:</b> ' + gr.getDisplayValue('approver') + '<br>');
template.print('<b>Approval State:</b> ' + gr.getValue('state') + '<br>');
template.print('<b>Approved Time:</b> ' + gr.getDisplayValue('sys_updated_on') + '<br>');
template.print('<b>Latest Comment:</b> ' + latestComment + '<br><br>');
} else {
template.print('No comments found from the previous approver.');
}
} else {
template.print('No approved records found.');
}
})(current, template, email, email_action, event);
Hope this will help you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2024 07:41 PM
Hi @Ankita Gupte ,
I have tested the code which I have given, its working in my pdi.
check the record which you are trying to send email have at least one approval
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2024 10:39 PM
Hi Runjay,
What if the approval is the first approval triggered after raising the request. It should show "No approval records found" but for second approval it should details of first approver.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2024 11:00 PM
Hi @Ankita Gupte ,
If you are triggering this notification after first approval then script should give you result.
First test the notification with exiting record: Test with record which have approval record and see whether it give you result or not. If it is giving then no issue at notification end you need to check when you are triggering this.
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2024 11:09 PM
Hi Runjay,
I am triggering this notification for all approvals. So for first approval it should send "No Approvals record found" and for next approvals is should send previous approvers details.