Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

How can I get all list of work notes in incident?

Laida Garcia
Kilo Explorer

I have created a button on incident using UI Action, it suppose to get all the value of work notes after clicking it. 

2 REPLIES 2

Ankur Bawiskar
Tera Patron

Hi,

you can take 2 approaches

Approach 1: query sys_journal_field

var arr = [];
var gr = new GlideRecord("sys_journal_field");
gr.addQuery("name", "incident");
gr.addQuery("element", "work_notes");
gr.query();
while (gr.next()) {
	arr.push(gr.getValue('value'));
}
gs.addInfoMessage("All work notes " + arr.toString());

OR

Approach 2:  UI Action should be server side

var notes = current.work_notes.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.addInfoMessage(na[i]);

regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Hitoshi Ozawa
Giga Sage

Hi Laida,

Following script will get the worknotes for the current incident.

var grIncident = new GlideRecord('sys_journal_field');
grIncident.addEncodedQuery('GOTOname=incident^element=work_notes^element_id=' + current.sys_id);
grIncident.orderBy('sys_created_on');
grIncident.query();
var workNotesArray = [];
while (grIncident.next()) {
  workNotesArray.push(grIncident.getValue('value').toString());
}
gs.info(workNotesArray);