Need to comment out using background script

Heisenberg
Tera Contributor

Hi,

Need to comment out particular line in  advance script using background script. I have around 2000 different script in the same table and have to comment out or remove the same line that is gs.log how can I do it using background script.

Please advice.

1 ACCEPTED SOLUTION

Hi,

If it is updating only 1 then I guess script is breaking or giving some exception while iterating; can you update code as below with the try catch block to handle the exception

try{

var gr = new GlideRecord('user_criteria');

gr.addQuery('script', 'CONTAINS', 'gs.log(');

//gr.setLimit(1);

gr.query();

if(gr.next()){

var script = gr.getValue('script');

script = script.replaceAll("gs.log(","//gs.log(");

gr.script = script;

gr.setWorkflow(false);
gr.autoSysFields(false);
gr.update();

}
}
catch(ex){
gs.info('Exception is: ' + ex);
}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

12 REPLIES 12

asifnoor
Kilo Patron

Hi,

It is not recommended to remove the log lines using some script. the script contains various types of code which includes different characters ((),;,/,'',"", etc and the script sometimes might break in between and could result in that specific script being corrupted.

considering you have 2000 such scripts, it is high risk job to do this with a script. I would rather suggest that you do this as part of maintenance and clean up few scripts at a time, may be the most used ones and cleanup those. Others, you can remove it slowly.

Mark the comment as a correct answer and helpful if this helps.

I have written a simple script which will remove the logs.. may be you can try this out if you want to remove the logs from the scripts completely.

var gr = new GlideRecord("user_criteria");
gr.addQuery("script", "CONTAINS", "gs.log(");
gr.query();
while(gr.next()) {
  var script = gr.getValue("script").toString();
  var script2="";
  script1 = script.split("\r\n");
  for(i=0;i<script1.length;i++) {
    if(!script1[i].trim().startsWith("gs.log")) {
        script2 = script2+"\r\n"+script1[i];
     }
  }
gr.script=script2;
gr.update();
}

Mark the comment as a correct answer and helpful if this helps.

Hi Asif,

if I setLimit to 100 its not working and additional gaps are getting updated in the beginning of all the script.

Please advice