Hi 

Try this code.

var gr = new GlideRecord("incident");
gr.addActiveQuery();
gr.addQuery("sys_id","9d3c1197c611228701cd1d94bc32d76d"); //for all active incidents,comment this line.
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 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.deleteRecord();
      sysaudit_rec.deleteRecord();
    }
  }
  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.deleteRecord();   
  }
}

Mark the comment as a correct answer and also helpful once worked.

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.

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();   
  }
}

View solution in original post

Allen Andreas
Tera Patron

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!

paulmorris
Giga Sage

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