Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Background script find and replace help

arobertson
Tera Guru

Hi all,

I'm after some assistance with a background script that i'm trying to get to work. I have a list of knowledge categories and i'm trying to bulk replace the word Dashboards with Analytics.

The field is a string field and i get the following list when running my background script query.

*** Script: KB: Dashboards / Dashboards / Connectivity

*** Script: KB: Dashboards / Dashboards / Other / General questions

*** Script: KB: Dashboards / Dashboards / Security / Remove Access

*** Script: KB: Dashboards / Dashboards / Connectivity / Access Issues

*** Script: KB: Dashboards / Dashboards / Other

*** Script: KB: Dashboards / Dashboards / Security / Access Issues

*** Script: KB: Dashboards / Dashboards / Security / New Access Request

*** Script: KB: Dashboards / Dashboards / Security

*** Script: KB: Dashboards / Dashboards

I have the following script. I have amended line 6 a few times to try different things, but cannot get it to update.

grCI = new GlideRecord('kb_category');

grCI.addQuery('full_category','STARTSWITH','Dashboards / D');

grCI.query();

while (grCI.next()){

gs.print('KB: '+grCI.full_category);

grCI.full_category = grCI.full_category.replace(/Dashboard/g , "Analytics");

grCI.update;

}

1 ACCEPTED SOLUTION

Got it to work, was missing () after the word update on line 7



grCI = new GlideRecord('kb_category');


grCI.addQuery('full_category','STARTSWITH','Dashboards / D');


grCI.query();


while (grCI.next()){


gs.print('KB 1: '+grCI.full_category);


grCI.full_category = grCI.full_category.replace(/Dashboards/g , "Analytics");


gs.print('KB 2: '+grCI.full_category);


grCI.update();


}


View solution in original post

6 REPLIES 6

Ct111
Tera Sage

Hi Alex,



I think in line 6 you have missed "s"   in Dashboards and n you are searching Dashboard which is why it's not executing . Correct it and let me know the output .




Please Hit/Like and Mark Helpful if it solved your purpose


Thanks for that, but it made no difference.



grCI = new GlideRecord('kb_category');


grCI.addQuery('full_category','STARTSWITH','Dashboards / D');


grCI.query();


while (grCI.next()){


gs.print('KB: '+grCI.full_category);


grCI.full_category = grCI.full_category.replace(/Dashboards/g , "Analytics");


grCI.update;


}



I get no error, just the same list re-printed back out.


Got it to work, was missing () after the word update on line 7



grCI = new GlideRecord('kb_category');


grCI.addQuery('full_category','STARTSWITH','Dashboards / D');


grCI.query();


while (grCI.next()){


gs.print('KB 1: '+grCI.full_category);


grCI.full_category = grCI.full_category.replace(/Dashboards/g , "Analytics");


gs.print('KB 2: '+grCI.full_category);


grCI.update();


}


mbhmbhmbh
Tera Contributor

Thanks for sharing this! We also have a need to perform bulk updates like this, for various reasons - very much appreciated!