Copy Work Notes From Problem To Incident

Community Alums
Not applicable

Hi,

I am writing Business Rules to Copy Work Notes from Problem to Incident. Description and Short Description work fine, but Work Notes is not working. Kindly help.

 

1.png

 

2.png

 

 

(function executeRule(current, previous /*null when async*/) {

var gr = new GlideRecord('incident');
gr.addQuery('problem_id', current.sys_id);
gr.query();
while(gr.next()){
	gr.work_notes = current.work_notes;
	gr.update();
}

})(current, previous);

 

 

3.png

 

4.png

 

Regards

Suman P.

1 ACCEPTED SOLUTION

Runjay Patel
Giga Sage

Hi @Community Alums ,

 

Work notes are stored in activity table not in activity incident and problem directly so it work like normal fields, try below code,

// Ensure the Incident record exists
if (current.incident) {
var incident = new GlideRecord('incident');
if (incident.get(current.incident)) {

// Query the problem's work notes (sys_journal_field)
var workNotes = new GlideRecord('sys_journal_field');
workNotes.addQuery('table', 'problem');
workNotes.addQuery('document', current.sys_id);
workNotes.addQuery('type', 'comment'); // Make sure it's Work Notes, not just any comments
workNotes.query();

// Loop through each Work Note and copy to the Incident
while (workNotes.next()) {
var newWorkNote = new GlideRecord('sys_journal_field');
newWorkNote.initialize();
newWorkNote.table = 'incident';
newWorkNote.document = incident.sys_id;
newWorkNote.type = 'comment'; // Ensure it's set as a Work Note
newWorkNote.value = workNotes.value; // Copy the actual work note text
newWorkNote.insert();
}
}
}

View solution in original post

1 REPLY 1

Runjay Patel
Giga Sage

Hi @Community Alums ,

 

Work notes are stored in activity table not in activity incident and problem directly so it work like normal fields, try below code,

// Ensure the Incident record exists
if (current.incident) {
var incident = new GlideRecord('incident');
if (incident.get(current.incident)) {

// Query the problem's work notes (sys_journal_field)
var workNotes = new GlideRecord('sys_journal_field');
workNotes.addQuery('table', 'problem');
workNotes.addQuery('document', current.sys_id);
workNotes.addQuery('type', 'comment'); // Make sure it's Work Notes, not just any comments
workNotes.query();

// Loop through each Work Note and copy to the Incident
while (workNotes.next()) {
var newWorkNote = new GlideRecord('sys_journal_field');
newWorkNote.initialize();
newWorkNote.table = 'incident';
newWorkNote.document = incident.sys_id;
newWorkNote.type = 'comment'; // Ensure it's set as a Work Note
newWorkNote.value = workNotes.value; // Copy the actual work note text
newWorkNote.insert();
}
}
}