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

Aravinthakumar
Tera Contributor

Can you please explain the use /D and /g in the above script for quering and replacing

?

/g modifier is Global search:

https://www.w3schools.com/jsref/jsref_regexp_g.asp

 

/D is non digit characters

https://www.w3schools.com/jsref/jsref_regexp_digit_non.asp

 

However, looking at his specific case, I think he's simply trying to match the name at the start of the string ie:

Dashboards / Dashboards / Connectivity

==

"Dashboards / D"ashboards / Connectivity