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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Heisenbery,

Can you explain in detail?

Are you saying you are having many background scrips with the line of code as gs.log() and you would like to comment that line of code from every background script which has that?

Regards

Ankur

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

Hi Ankur,

I have a many user criteria with script in that by mistake I have added gs.log in it which needs to be commented out, can I do it through background script.

Hi Heisenbery,

Somewhat trickier to handle this; since it is a script field and you will have to search for the text gs.log( in that complete string and replace the text with //gs.log(

try this script below and it would search user_criteria table where script contains gs.log and will replace with //gs.log :

Try first with setLimit(1) and check whether it got changed and then comment out that line

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

}

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

Hi Ankur,

 

It updates only 1 record, even after removing setlimit, can you help what changes could be made on this.