Get latest 3 work notes in email

TanayaGavande
Tera Expert

Hi All,

 

I want to add the latest 3 work notes from the project task table to an email. I understand I cannot do this directly, so I created an email script:

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
 
    // Add your code here
    var wn = '';
    var gr = new GlideRecord("pm_project_task");
    gr.addQuery("sys_id", current.sys_id);
    gr.query();
    if (gr.next()) {
        wn = gr.work_notes.getJournalEntry(3); // Get latest 3 work notes
    }
    template.print(wn);
 
})(current, template, email, email_action, event);

 

 While this is working (I get the last 3 work notes), but it does not add proper formatting. e.g.:

TanayaGavande_0-1733403674235.png

Is there any way I can add the OOTB formatting, i.e. one work note after the other?

Thank you.

1 ACCEPTED SOLUTION

Hi @Ankur Bawiskar ,

I made a few customizations to make it look like the OOTB notes. Sadly, there isn't a way to get around doing GR for the User table, for the user name. Here's the code I used:

 

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

View solution in original post

8 REPLIES 8

Uncle Rob
Kilo Patron

OOB there's Email Scripts called incident _comment and incident_latest_comment.
I'd look deep into those.  It appears they do the formatting manually in the script.

 

Hi @Uncle Rob 

Thanks a lot for this insight. The script in these email scripts is helpful to get an insight into the HTML code used in an OOTB way.

Ankur Bawiskar
Tera Patron
Tera Patron

@TanayaGavande 

you can get last 3 journal entry using getJournalEntry(3)

but you will have to process it to separate and get the actual work notes

this will work as it will get those individually

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

    // Add your code here
    var gr = new GlideRecord("sys_journal_field");
    gr.addEncodedQuery("element=work_notes^element_id=" + current.sys_id);
    gr.query();
    while (gr.next()) {
        var wn = gr.value.toString();
        template.print(wn + "<br/>");
    }

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

@TanayaGavande 

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