Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to get the latest worknotes value

vinuth v
Tera Expert

Hi All,

 

How to get the latest worknotes value(Information),

I tried with the background script like below and I got the latest value through background script as I expected,

 

    var workNotes = " ";
    var workNotesGR = new GlideRecord('sys_journal_field');
    workNotesGR.addQuery('element_id', 'b9c5b5ee872a06d0c461fdd7cebb35df');
    workNotesGR.addQuery('element', 'work_notes');
   workNotesGR.orderByDesc('sys_created_on');
   workNotesGR.orderBy('sys_created_on');
    workNotesGR.query();
    if(workNotesGR.next()) {
        workNotes = workNotesGR.getValue('value');
        gs.log("worknotesvinuth18" +workNotes);
        }
Updated latest worknote as "test4" and through background script get the same value.
 vinuthv_2-1717743881237.png

 

 
Same script I tried in the before update Business rule but it's not working as expected (and I am getting the last but one value in the logs)
 
var taskGR = new GlideRecord('sc_task');
taskGR.addQuery('number', current.number);
 taskGR.query();
while (taskGR.next()) {
var assign=taskGR.assigned_to.getDisplayValue();
    gs.log("testvinuth9 "+assign);
    var workNotes = '';
    var workNotesGR = new GlideRecord('sys_journal_field');
    workNotesGR.addQuery('element_id', taskGR.sys_id);
    workNotesGR.addQuery('element', 'work_notes');
   workNotesGR.orderByDesc('sys_created_on');
   workNotesGR.orderBy('sys_created_on');
    workNotesGR.query();
    if(workNotesGR.next()) {
        workNotes = workNotesGR.getValue('value');
        gs.log("worknotesvinuth18" +workNotes);
}
        vinuthv_0-1717743493467.png
Here I am getting the test2 instead of test3 value in the worknotes.
vinuthv_1-1717743538483.png

 

 Please any one suggest me how to get the latest updated work notes value(Information) from the Business Rule.
 
Thanks in advance,
Vinuth
 
   
   
1 REPLY 1

dgarad
Giga Sage

Hi @vinuth v 

refer to the below code.

var notes = current.work_notes.getJournalEntry(-1);    // -1 for all the work notes value , 1 for latest work notes value

var notes = current.comments.getJournalEntry(-1);   
If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad