Get previous approval comments in approval notification of RITM in email notification

Ankita Gupte
Kilo Sage

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.

 

1 ACCEPTED SOLUTION

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!

View solution in original post

15 REPLIES 15

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!