Pulling formatted last comment in RITM into the email notification

MWright1
Giga Guru

Hi all,

 

I am having a hard time pulling the last comment into a notification email for my users.

 

When I use the ${comments} to add the comments, it automatically pulls ALL the comments.  I only want the last comment made.

 

I found this code and it works except that it removes any formatting in the text so it all looks like mashed up garbage especially if the comment was created because it came from an email.

var fieldName = 'comments';
    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);
    }

Is there any way for me to keep the formatting?  When I view the actual journal entries, it does have the full formatted text.

I have also tried (unsuccessfully):

if (rec.next()) {
        template.print(rec.getDisplayValue);
}

 

Please help.

Thanks.

 

 

1 ACCEPTED SOLUTION

@MWright1  This method is documented and you should be able to split it up like this

https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server/no-namespace/c_GlideElem...


//gets all journal entries as a string where each entry is delimited by '\n\n'

var notes = current.comments.getJournalEntry(-1);

//stores each entry into an array of strings

var na = notes.split("\n\n");

for (var i = 0; i < na.length; i++){

gs.info(na[i]);

}

View solution in original post

6 REPLIES 6

Jake Sadler
Kilo Sage

Hey @MWright1 ,

 

Something like this should work

 var comment = current.comments.getJournalEntry(1);
template.print(comment);

Same result...  All the formatting is gone so the whole email looks like a bunch of unreadable text... all concatenated.  I need to keep the formatting so the message is readable.  Thanks for trying.

@MWright1  This method is documented and you should be able to split it up like this

https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server/no-namespace/c_GlideElem...


//gets all journal entries as a string where each entry is delimited by '\n\n'

var notes = current.comments.getJournalEntry(-1);

//stores each entry into an array of strings

var na = notes.split("\n\n");

for (var i = 0; i < na.length; i++){

gs.info(na[i]);

}

Thanks, Jake.

I was able to make that work!  I used 1 instead of -1 and put some wrapping to split up the lines:

 

var sComment = current.comments.getJournalEntry(1);
var nb;
var na = sComment.split("\n\n");
for (var i = 0; i < na.length; i++) {
    template.print('<p><font size="2" color="#808080" face="helvetica">');
    nb = na[i].split('\n');
    for (var n = 0; n < nb.length; n++){
	template.print(nb[n]);
	template.print('<br />');
        if (i == 0) {
            template.print('<br />');
        }
    }
    template.print('</font></p>');
}

 

Thanks again!