getJournalEntry() to return the latest comment added - script logic not working

chatsaurav19
Tera Contributor

Hi All,

 

Through a script include, I am trying to return the latest journal entry but it is not working. Can someone please help to correct the script?

 

var arr = [];

var gr1 = new GlideRecord('table');
gr1.addQuery('sy_id', this.getParameter('sysparm_roleID'));
gr1.query();
if (gr1.next()) {

arr.push(gr1.getDisplayValue('role_title'));
arr.push(gr1.level.getDisplayValue());

arr.push(gr1.comment.getJournalEntry(-1)); //Comment is the field name which is journal type; Not Working

}

return JSON.stringify(arr);

 

chatsaurav19_0-1691081384386.png

 

 

Thanks and Regards,

Saurabh Chatterjee

 

7 REPLIES 7

Hi @Harish KM @sushantmalsure 

 

It is returning the value marked in yellow tick "Test" but I want the latest one "Test Again" to get returned.

 

chatsaurav19_0-1691140784586.png

Thanks and Regards,

Saurabh Chatterjee

 

getJournalEntry(1)

is meant to give you latest comment, use it with comments as field.

gr1.comments.getJournalEntry(1)

 

I have tested it on PDI and it worked for me. try testing on new task . Do share the script screenshot if that didn't work.

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,Sushant Malsure

Ankur Bawiskar
Tera Patron
Tera Patron

@chatsaurav19 

why not query sys_journal_field and pick the latest value for your record and fieldname?

var fieldName = 'comments';
var latestValue;
var rec = new GlideRecord('sys_journal_field');
rec.orderByDesc('sys_created_on');
rec.addQuery('name', 'tableName');
rec.addQuery('element', fieldName);
rec.addQuery('element_id', this.getParameter('sysparm_roleID'));
rec.setLimit(1);
rec.query();
if(rec.next()){
	latestValue = rec.value;
}

var arr = [];
var gr1 = new GlideRecord('table');
gr1.addQuery('sy_id', this.getParameter('sysparm_roleID'));
gr1.query();
if (gr1.next()) {
	arr.push(gr1.getDisplayValue('role_title'));
	arr.push(gr1.level.getDisplayValue());
	arr.push(latestValue);
}

return JSON.stringify(arr);

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