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

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

Ankur Bawiskar
Tera Patron
Tera Patron

@KARUNK 

No system property change required.

In your notification include this email script and it will show latest 3 work notes

I hope you are aware on how to include email script in email body

(function runMailScript(current, template, email, email_action, event) {

    // Add your code here
    template.print('Latest 3 work notes <br/>');
    var fieldName = 'work_notes';
    var rec = new GlideRecord('sys_journal_field');
    rec.orderByDesc('sys_created_on');
    rec.addQuery('name', current.getTableName());
    rec.addQuery('element', fieldName);
    rec.addQuery('element_id', current.sys_id);
    rec.setLimit(1);
    rec.query();
    if (rec.next()) {
        template.print(rec.value);
    }

})(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

@KARUNK 

Thank you for marking my response as helpful.

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

@KARUNK 

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

Hi @Ankur Bawiskar I tried using this email script, but I'm only getting one work note populated instead of three. I changed rec.setLimit(1) to rec.setLimit(3), but it's still showing only one work note