Some PDIs are currently unavailable, and PDI actions are paused. View the latest updates here. Read More

when an HR Agent clicks compose Email, automatically include the last 5 comments in workspace

PravallikaB7459
Tera Contributor

When an HR Agent clicks the out-of-the-box "Compose Email" action, the email body should automatically include the last 5 comments from the HR Case.

What I have tried:

Created an Email Client Template.
Added a Mail Script to retrieve the last 5 comments.
Referenced the Mail Script in the Email Client Template.
However, when I click Compose Email from HR Agent Workspace:

The Mail Script does not execute.
The email body does not populate the last 5 comments.

1 REPLY 1

Ankur Bawiskar
Tera Patron

@PravallikaB7459 

notification mail script does get trigger from email client template

here is the working logic

Seems something in your side is not configured properly or script is breaking

Email Script:

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

    // Add your code here

    var fieldName = 'work_notes';

    var arr = [];
    var rec = new GlideRecord('sys_journal_field');
    rec.orderByDesc('sys_created_on');
    rec.addQuery('name', 'change_request');
    rec.addQuery('element', fieldName);
    rec.addQuery('element_id', current.sys_id);
    rec.setLimit(5);
    rec.query();
    while (rec.next()) {
        arr.push(rec.value.toString());
    }
    template.print('Latest 5 work notes are' + arr.toString());

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

AnkurBawiskar_1-1785144056436.png

Email Client Template calling that Email Script:

AnkurBawiskar_2-1785144086544.png

 

Output:

AnkurBawiskar_3-1785144113905.png

 

AnkurBawiskar_0-1785144030753.png

 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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