Need to get the value of the rejection comments

Abdul
Tera Contributor

Hi,

 

I need to get the comments of the approval table when the approval is rejected for a catalog item.

i have created an email script.

The catalog item is Outlook Mailbox Permissions Request.

When an approval is rejected, the comments has to be sent to the requested user.

I have created an event and called that in the workflow to trigger the notification. the comments with which the user is rejecting the request has to be sent via email 

Below is the code please help

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    // Add your code here

    var comment = '';
    var gr = new GlideRecord('sysapproval_approver');
    gr.addQuery('sysapproval', current.sys_id);
    gr.addQuery('state', 'rejected');
    gr.query();
    if (gr.next()) {
        comment = gr.comments.toString();
    }

    template.print(comment);

})(current, template, email, email_action, event);
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Abdul 

event is on which table, notification is on which table?

please share some screenshots.

this will fetch the rejection comments if your notification is on RITM table

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    // Add your code here

    var comment = '';
    var gr = new GlideRecord('sysapproval_approver');
    gr.addQuery('sysapproval', current.sys_id);
    gr.addQuery('state', 'rejected');
    gr.query();
    if (gr.next()) {
        comment = gr.comments.getJournalEntry(1);
    }

    template.print(comment);

})(current, template, email, email_action, event);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

7 REPLIES 7

Muhammad Salar
Giga Sage

Hi @Abdul , 
You will have to query sys_journal_field for comments, try this script

(function runMailScript(current, template, email, email_action, event) {
    var comment = '';

    var approverGr = new GlideRecord('sysapproval_approver');
    approverGr.addQuery('sys_id', current.sys_id); // Replace with Rejected Approval Record Sysid
    approverGr.addQuery('state', 'rejected');
    approverGr.query();
    if (approverGr.next()) {
        var journalGr = new GlideRecord('sys_journal_field');
        journalGr.addQuery('element_id', approverGr.sys_id);
        journalGr.addQuery('element', 'comments');
        journalGr.orderByDesc('sys_created_on');
        journalGr.query();
        if (journalGr.next()) {
            comment = journalGr.value;
        }
    }

    if (!comment) {
        comment = 'No rejection comments were provided.';
    }

    template.print("Your request was rejected with the following comment:<br><br>" + comment);

})(current, template, email, email_action, event);

Ankur Bawiskar
Tera Patron
Tera Patron

@Abdul 

event is on which table, notification is on which table?

please share some screenshots.

this will fetch the rejection comments if your notification is on RITM table

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    // Add your code here

    var comment = '';
    var gr = new GlideRecord('sysapproval_approver');
    gr.addQuery('sysapproval', current.sys_id);
    gr.addQuery('state', 'rejected');
    gr.query();
    if (gr.next()) {
        comment = gr.comments.getJournalEntry(1);
    }

    template.print(comment);

})(current, template, email, email_action, event);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Abdul 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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