- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-13-2019 06:27 AM
Hi All,
On incident table, I want to copy additional comments to work notes and delete the additional comments . I want to do this as one time activity so looking to write background script for this. I tried below script for one incident ( I need to do it for all Incs) but it did not work
var gr = new GlideRecord('sys_journal_field')
gr.addQuery('element_id','f0236147dbd57b40d821f91ebf961955');
gr.query();
if(gr.next()){
gr.work_notes = gr.work_notes + gr.comments
gr.comments ="";
gr.update();
}
Can someone please help ?
Thanks!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-14-2019 04:41 AM
Hi,
Updated the code slightly. Try this.
var gr = new GlideRecord("incident");
gr.addActiveQuery();
gr.query();
while(gr.next()) {
var notes = gr.comments.getJournalEntry(-1);
var na = notes.split("\n\n"); //stores each entry into an array of strings
gs.print("Entered while loop"+notes);
for (var i = 0; i < na.length; i++) {
gs.print(na[i]);
gr.work_notes=na[i];
gr.update();
}
//now delete the additional comments. which we need to do from 3 tables sys_audit, sys_history_line,sys_history_set and sys_journal_field
var sysaudit_rec = new GlideRecord("sys_audit");
sysaudit_rec.addQuery("documentkey",gr.sys_id); //incident sys_id
sysaudit_rec.query();
while(sysaudit_rec.next()) {
//now fetch and delete associated history line records
var history_rec = new GlideRecord('sys_history_line')
history_rec.addQuery('audit_sysid',sysaudit_rec.sys_id);
history_rec.query();
if(history_rec.next()){
history_rec.deleteMultiple();
}
sysaudit_rec.deleteMultiple();
}
//fetch and delete entries from sys_history_set.
var historyset_rec = new GlideRecord('sys_history_set')
historyset_rec.addQuery('id',gr.sys_id); //incident sys_id
historyset_rec.query();
gs.print("entered into historyset record"+gr.sys_id);
if(historyset_rec.next()){
historyset_rec.deleteMultiple();
}
var journal_rec = new GlideRecord("sys_journal_field");
journal_rec.addQuery("element_id",gr.sys_id); //incident sys_id
journal_rec.addQuery("element","comments");
journal_rec.query();
while(journal_rec.next()) {
journal_rec.deleteMultiple();
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-14-2019 03:44 AM
Hi,
Try this code and it should work.
var gr = new GlideRecord("incident");
gr.addActiveQuery();
gr.query();
while(gr.next()) {
var notes = gr.comments.getJournalEntry(-1);
var na = notes.split("\n\n"); //stores each entry into an array of strings
gs.print("Entered while loop"+notes);
for (var i = 0; i < na.length; i++) {
gs.print(na[i]);
gr.work_notes=na[i];
gr.update();
}
//now delete the additional comments. which we need to do from 3 tables sys_audit, sys_history_line,sys_history_set and sys_journal_field
var sysaudit_rec = new GlideRecord("sys_audit");
sysaudit_rec.addQuery("documentkey",gr.sys_id); //incident sys_id
sysaudit_rec.query();
while(sysaudit_rec.next()) {
//now fetch and delete associated history line records
var history_rec = new GlideRecord('sys_history_line')
history_rec.addQuery('audit_sysid',sysaudit_rec.sys_id);
history_rec.query();
if(history_rec.next()){
history_rec.deleteMultiple();
}
//fetch and delete entries from sys_history_set.
var historyset_rec = new GlideRecord('sys_history_set')
historyset_rec.addQuery('id',gr.sys_id); //incident sys_id
historyset_rec.query();
if(historyset_rec.next()){
historyset_rec.deleteMultiple();
}
sysaudit_rec.deleteMultiple();
}
var journal_rec = new GlideRecord("sys_journal_field");
journal_rec.addQuery("element_id",gr.sys_id); //incident sys_id
journal_rec.addQuery("element","comments");
journal_rec.query();
while(journal_rec.next()) {
journal_rec.deleteMultiple();
}
}
Mark the comment as a correct answer and also helpful once worked.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-14-2019 04:41 AM
Hi,
Updated the code slightly. Try this.
var gr = new GlideRecord("incident");
gr.addActiveQuery();
gr.query();
while(gr.next()) {
var notes = gr.comments.getJournalEntry(-1);
var na = notes.split("\n\n"); //stores each entry into an array of strings
gs.print("Entered while loop"+notes);
for (var i = 0; i < na.length; i++) {
gs.print(na[i]);
gr.work_notes=na[i];
gr.update();
}
//now delete the additional comments. which we need to do from 3 tables sys_audit, sys_history_line,sys_history_set and sys_journal_field
var sysaudit_rec = new GlideRecord("sys_audit");
sysaudit_rec.addQuery("documentkey",gr.sys_id); //incident sys_id
sysaudit_rec.query();
while(sysaudit_rec.next()) {
//now fetch and delete associated history line records
var history_rec = new GlideRecord('sys_history_line')
history_rec.addQuery('audit_sysid',sysaudit_rec.sys_id);
history_rec.query();
if(history_rec.next()){
history_rec.deleteMultiple();
}
sysaudit_rec.deleteMultiple();
}
//fetch and delete entries from sys_history_set.
var historyset_rec = new GlideRecord('sys_history_set')
historyset_rec.addQuery('id',gr.sys_id); //incident sys_id
historyset_rec.query();
gs.print("entered into historyset record"+gr.sys_id);
if(historyset_rec.next()){
historyset_rec.deleteMultiple();
}
var journal_rec = new GlideRecord("sys_journal_field");
journal_rec.addQuery("element_id",gr.sys_id); //incident sys_id
journal_rec.addQuery("element","comments");
journal_rec.query();
while(journal_rec.next()) {
journal_rec.deleteMultiple();
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-13-2019 07:11 AM
Hi,
You can get the past additional comments by using something like:
gr.comments.getJournalEntry(1); (1) would just get to last comment, whereas -1 would get them all.
As far as then deleting ALL past comments...that's going to require you to go through another table, I'd believe as they don't live solely on the incident table.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-14-2019 01:44 AM
Hey,
This should work
//Testing for one record
var gr = new GlideRecord('sys_journal_field')
gr.addQuery('element_id','f0236147dbd57b40d821f91ebf961955');
gr.addQuery('element','comments');
gr.query();
while (gr.next()){
gr.element = "work_notes";
gs.getInfoMessage('asdasd');
gr.update();
}
//Copy all Incident comments to work notes
var gr = new GlideRecord('sys_journal_field')
gr.addQuery('name','incident');
gr.addQuery('element','comments');
gr.query();
while (gr.next()){
gr.element = "work_notes";
gs.getInfoMessage('asdasd');
gr.update();
}
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022