Replace String not working

kemmy1
Tera Guru

After a clone, we would like to use a clone clean up script to update the sys_app_module records that have a URL in them.

var instance = gs.getProperty('instance_name');
    var gr = new GlideRecord('sys_app_module');
    gr.addEncodedQuery('link_type=DIRECT^querySTARTSWITHhttps://abc.service-now');
    gr.query();
    while(gr.next()){
        gr.query.replace('https://abc.service-now', 'https://' + instance + '.servicenowservices.com');
        gr.update();
    }
}

I swear this used to work and now nothing.

Am I doing something wrong?

Lisa

 

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Kemmy,

 

Replace   gr.query.replace('https://abc.service-now', 'https://' + instance + '.servicenowservices.com'); with  below  line

gr.query  = gr.query.replace('https://abc.service-now', 'https://' + instance + '.servicenowservices.com');

 

-Pradeep Sharma 

View solution in original post

2 REPLIES 2

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Kemmy,

 

Replace   gr.query.replace('https://abc.service-now', 'https://' + instance + '.servicenowservices.com'); with  below  line

gr.query  = gr.query.replace('https://abc.service-now', 'https://' + instance + '.servicenowservices.com');

 

-Pradeep Sharma 

Alok Das
Tera Guru

Hi,

As Pradeep suggested you need to modify the piece of code after which your code will look like:

var instance = gs.getProperty('instance_name');
var gr = new GlideRecord('sys_app_module');
gr.addEncodedQuery('link_type=DIRECT^querySTARTSWITHhttps://abc.service-now');
gr.query();
while(gr.next()){
	gr.query= gr.query.replace('https://abc.service-now', 'https://' + instance + '.servicenowservices.com');
	gr.update();
}

However, not sure why the instance name is hard coded in your module. Instead of hard coding why not just use the argument and let the current instance URL populate automatically.

This will save you from running this script every time you clone.

Just remove "https://abc.service-now" from a arguments of module and give try, I believe it will automatically pick the current instance url before the argument.

Kindly mark my answer as Correct and Helpful based on the Impact.

Regards,

Alok