copy additional comments to work notes

Learn SN
Tera Contributor

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!

 

1 ACCEPTED SOLUTION

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

8 REPLIES 8

asifnoor
Kilo Patron

Hi,

Try this code. This will copy all the additional comments as worknotes.

var gr = new GlideRecord("incident");
gr.addActiveQuery();
gr.addQuery("sys_id","e8caedcbc0a80164017df472f39eaed1"); //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
  for (var i = 0; i < na.length; i++) {
    gs.print(na[i]);
    gr.work_notes.setJournalEntry(na[i]); 
  }
}

Mark the comment as a correct answer and also helpful if it answers your question.

Hi Asif,

 

This is not working:(

Thanks!

 

Try this once

var gr = new GlideRecord("incident");
gr.addActiveQuery();
gr.addQuery("sys_id","e8caedcbc0a80164017df472f39eaed1"); //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
  for (var i = 0; i < na.length; i++) {
    gs.print(na[i]);
    gr.work_notes=na[i]; 
    gr.update();
  }
}

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.