How to Include the Latest 3 Work Notes in Email Notifications

KARUNK
Tera Contributor

I’m working on a requirement where we need to send an email notification from a task (Change) that includes the latest 3 work notes in the email body.

Has anyone implemented a similar solution? I’m looking for the best approach to achieve this—whether through a notification template, scripted email, or email script.

1 ACCEPTED SOLUTION

Ehab Pilloor
Mega Sage

Hi @KARUNK,

By default, glide.email.journal.lines is set to 3 but that includes Additional Comments too.

Use this email script to retrieve last 3 lines of Worknotes.

Note: This will work on Record insert or update.

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

    var work_note = new GlideRecord("sys_journal_field");
    work_note.addEncodedQuery("element_id=" + current.sys_id + "^element=work_notes");
    work_note.orderByDesc('sys_created_on');
    work_note.setLimit(3);
    work_note.query();
    while (work_note.next()) {
        var user = new GlideRecord("sys_user");
        user.addQuery("email", work_note.sys_created_by);
        user.query();
        if (user.next()) {
            template.print('<hr>');
            template.print('<div style="display: flex; justify-content: space-between; align-items: center;">');
            template.print('<span><b>' + work_note.sys_created_on + ' - ' + user.name + '</b></span>');
            template.print('<span>(Work Notes (Internal))</span>');
            template.print('</div>' + work_note.value + '<br>');
        }
    }
})(current, template, email, email_action, event);

Regards,

Ehab Pilloor

View solution in original post

10 REPLIES 10

@KARUNK 

it should work fine.

Do you have minimum 3 work notes on that record?

what debugging did you do?

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