- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2024 07:11 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2024 07:34 AM - edited 08-02-2024 07:55 AM
@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]);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2024 05:18 AM - edited 08-09-2024 06:31 AM
Hi @MWright1
Please feel free to try the notification email script below. It's pulling the formatted last/latest comment like OOB formatting.
Notification Email Script:
template.print("<hr>");
template.print('\n');
var sComment = current.comments.getJournalEntry(1);
var sp1, sp2;
sp1 = sComment.split('\n');
for (var i = 0; i < sp1.length; i++) {
if (i == 0) {
sp2 = sp1[i].split("(");
template.print('<div><span><strong>');
template.print(sp2[0]);
template.print('</strong></span>');
template.print('<span style="float:right;"><sup>');
template.print("Additional comments");
template.print('</sup></span></div>');
}
if (i != 0) {
template.print(sp1[i]);
template.print('<br />');
}
}
Please let me know your views and Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks,
Jyoti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2024 05:31 AM
Thanks, Jyoti. I will try it out.
M