- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2020 05:19 PM
I'm trying to print the last comment from an RITM into an email notification. When I use current.comments.getJournalEntry(1), it returns "Request Automatically Closed as all Line Items were complete." Assuming this is automatically added upon completion... can anyone help with a script to print the last relevant comment entered by the fulfiller?
Thank you,
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2020 06:01 PM
Hi
As all journal entries store in sys_journal_field table so you can query that table and get the second comment. Try something like below.
var count = 0;
var secondLastComment = '';
var gr = new GlideRecord('sys_journal_field');
gr.addQuery('element_id', current.sys_id);
gr.addQuery('name', 'sc_req_item');
gr.orderByDesc('sys_created_on');
gr.setLimit(2);
gr.query();
while(gr.next()){
count = count + 1;
if(count == 2)
secondLastComment = gr.getValue('value');
}
gs.info(secondLastComment);
I didn't tested this code but I am sure this will direct you in the right direction.
Thanks & Regards,
Sharjeel
Muhammad

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2020 06:01 PM
Hi
As all journal entries store in sys_journal_field table so you can query that table and get the second comment. Try something like below.
var count = 0;
var secondLastComment = '';
var gr = new GlideRecord('sys_journal_field');
gr.addQuery('element_id', current.sys_id);
gr.addQuery('name', 'sc_req_item');
gr.orderByDesc('sys_created_on');
gr.setLimit(2);
gr.query();
while(gr.next()){
count = count + 1;
if(count == 2)
secondLastComment = gr.getValue('value');
}
gs.info(secondLastComment);
I didn't tested this code but I am sure this will direct you in the right direction.
Thanks & Regards,
Sharjeel
Muhammad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2020 05:35 PM
Thanks! This wasn't pulling any comments when I tested, so I made a slight modification to reference the RITM sys id, and it worked great.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2021 07:32 PM
Hi @MB, can you explain where you made your change in the query to reference the RITM sys ID. I thought it would be here on this line
gr.addQuery('element_id', current.sys_id);
but it's still not displaying comments for me regardless of how I change it to the reference the request_item or sc_req_item. I've tried many variations but it's not displaying any comments at all still. Would you be able to paste your code in? Is this getting the 2nd to last Additional Comment made?
Thanks!
PT
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-17-2022 10:30 AM
Hey there!
I wrote an article (and free tool) on how to get Journal entries from a given record and (optionally, depending on how you call the function in the article) parse and convert/sanitize the contents of each entry for HTML.
For example, by calling the function in the article, in the following way, in a Notification Mail Script, you can get all journal entries formatted as HTML:
var arrCommentsAndWorknotes = getJournalEntries(
current,
'comments_and_work_notes',
false,
false,
-1 //Get ALL journal entries
);
There's also some code near the bottom of the article that'll convert the result into an HTML table for you as well. 🙂
Once you've got the array of values, the last comment will be at the index (arrayOfJournalEntries.length - 1)
, which means that the second-to-last will be at the index (arrayOfJournalEntries.length - 2)
(since arrays use a zero-based index). 🙂
For example:
function testThing() {
var myArray = [
'first',
'second',
'second-to-last',
'last'
];
var arrLength = myArray.length;
var lastIndex = (arrLength - 1);
var secondToLastIndex = (arrLength - 2); //redundant, but descriptive.
//Because arrays use a zero-based index...
console.log('The first element is: ' + myArray[0]);
console.log('The last element is: ' + myArray[lastIndex]);
console.log('The SECOND-TO-LAST element is: ' + myArray[secondToLastIndex]);
}
testThing();
Output:
The first element is: first
The last element is: last
The SECOND-TO-LAST element is: second-to-last
The article is at https://go.snc.guru/journal.
(Note: You can now create your own short-URLs using https://go.snc.guru/short!)
And don't forget - If you found this helpful, please click to mark it as helpful. It really helps out! 🙂