
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2019 02:36 AM
Im struggling with copying comments and work notes from one incident record to a newly created incident task record. When copying from the incident to an already existing record (Update Business Rule) there is no problem.
But if I write for example 2 comments and 1 work note in an incident, then create the incident task record, all comments and work notes are copied to the new record without their original timestamp. The Insert Business Rule is first copying the work notes, then comments, thus putting the work notes first and comments last, which messes with the time order when they were written. See pictures:
Incident record:
Incident Task record:
Since I haven't found out any way on how to preserve the timestamps (any ideas?) I thought of creating a business rule that copies all comments and work notes and put them in 1 work note through an array. See example:
But then the problem is that I don't know how to copy both work notes and comments with getJournalEntry(-1) into 1 array. It seems that I have to put comments and work notes in their own arrays (?), creating the same issue as I already having.
Example taken from ServiceNow documentation, getting comments but not work notes into an array:
var gr = new GlideRecord('incident');
gr.addQuery('number','INC0032322');
gr.query();
if(gr.next()) {
var notes = gr.comments.getJournalEntry(-1);
//gets all journal entries as a string where each entry is delimited by '\n\n'
var na = notes.split("\n\n");
//stores each entry into an array of strings
for (var i = 0; i < na.length; i++)
gs.print(na[i]);
};
So to summarize, my questions are:
1. Is it possible to preserve the timestamp of the comments/work notes so that they are inserted in the right order?
2. If not so, is it then possible to get both comments and work notes in the right order into 1 array and using that to create 1 work note with all the comments and work notes in the right order?
Thanks
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2019 07:47 AM
Something like this should work to print out all the comments and work notes in the correct order.
(The code could be prettier, but it does the job.)
var allcomments = [];
var gr = new GlideRecord('incident');
gr.addQuery('number', 'INC0009009');
gr.query();
if (gr.next()) {
var current_comment = {};
var current_work_note = {};
var commentsStr = gr.comments.getJournalEntry(-1);
var work_notesStr = gr.work_notes.getJournalEntry(-1);
//gets all journal entries as a string where each entry is delimited by '\n\n'
var comments = commentsStr.split("\n\n");
var work_notes = work_notesStr.split("\n\n");
var work_note_date;
var comment_date;
for (var i = 0; i < comments.length; i++) {
current_comment = {};
if (comments[i] != '') {
current_comment.type = 'comment';
current_comment.text = comments[i];
comment_date = comments[i].split(' ');
current_comment.date = comment_date[0] + ' ' + comment_date[1];
allcomments.push(current_comment);
}
}
for (var x = 0; x < work_notes.length; x++) {
current_work_note = {};
if (work_notes[x] != '') {
current_work_note.type = 'work_note';
current_work_note.text = work_notes[x];
work_note_date = work_notes[x].split(' ');
current_work_note.date = work_note_date[0] + ' ' + work_note_date[1];
allcomments.push(current_work_note);
}
}
allcomments.sort(function (a, b) {
if (GlideDateTime(a.date) < GlideDateTime(b.date)) return 1;
if (GlideDateTime(a.date) > GlideDateTime(b.date)) return -1;
return 0;
});
for (var y = 0; y < allcomments.length; y++) {
//gs.print(allcomments[y].type);
gs.print(allcomments[y].text);
//gs.print(allcomments[y].date);
}
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2019 02:57 AM
We got something like this working to merge a duplicate incident with an existing one, we wanted to add the comments from the merged incident as work notes on the existing incident but preserve the sequence of events.
We had to query the sys_history_set table to get the history set for the incident to be merged, use that to query the sys_history_line table and get the additional comments and loop through them. For each one we got the sys_history_set record for the existing record, inserted a record into the sys_audit table to create a relationship to the existing incident and then inserted a copy of the original sys_history_line record with some of the values amended to associate it with the existing record as a work note...
It was a bit complex...

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2019 04:30 AM
It sounds quite complex. Maybe I will try to get the client to work in one record, not in two records
Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2019 07:47 AM
Something like this should work to print out all the comments and work notes in the correct order.
(The code could be prettier, but it does the job.)
var allcomments = [];
var gr = new GlideRecord('incident');
gr.addQuery('number', 'INC0009009');
gr.query();
if (gr.next()) {
var current_comment = {};
var current_work_note = {};
var commentsStr = gr.comments.getJournalEntry(-1);
var work_notesStr = gr.work_notes.getJournalEntry(-1);
//gets all journal entries as a string where each entry is delimited by '\n\n'
var comments = commentsStr.split("\n\n");
var work_notes = work_notesStr.split("\n\n");
var work_note_date;
var comment_date;
for (var i = 0; i < comments.length; i++) {
current_comment = {};
if (comments[i] != '') {
current_comment.type = 'comment';
current_comment.text = comments[i];
comment_date = comments[i].split(' ');
current_comment.date = comment_date[0] + ' ' + comment_date[1];
allcomments.push(current_comment);
}
}
for (var x = 0; x < work_notes.length; x++) {
current_work_note = {};
if (work_notes[x] != '') {
current_work_note.type = 'work_note';
current_work_note.text = work_notes[x];
work_note_date = work_notes[x].split(' ');
current_work_note.date = work_note_date[0] + ' ' + work_note_date[1];
allcomments.push(current_work_note);
}
}
allcomments.sort(function (a, b) {
if (GlideDateTime(a.date) < GlideDateTime(b.date)) return 1;
if (GlideDateTime(a.date) > GlideDateTime(b.date)) return -1;
return 0;
});
for (var y = 0; y < allcomments.length; y++) {
//gs.print(allcomments[y].type);
gs.print(allcomments[y].text);
//gs.print(allcomments[y].date);
}
};

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2019 04:36 AM
Works really well!