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 to update the prefix of existing records in a table

mitzaka
Mega Guru

Hi SNC,

I want to change the prefix on a table, and along with that to update all of the existing records in the table with the new prefix. The table is change_task, where the old prefix is CTASK. I want to replace it with CHGTASK.

I am trying with this background script, however I cannot seem to get them updated. Do you see anything wrong about it?

addPrefix();  

function addPrefix() {  

var x = 0;  

var pre = '';  

var num = '';  

var len = '';  

var gr = new GlideRecord('change_task');  

gr.query();  

while (gr.next()) {  

gr.setWorkflow(false); // so no business rules are run  

pre = gr.number.toString().substr(0,5); //first 5 chars of original number   to get CTASK

num = gr.number.toString().substr(5); //remaining chars of number

len = gr.number.toString().length;

if (pre == 'CTASK' && len < 11){ //11 as the chars are 12, counting 0 position

for (x = 0; x < (11-len); x++){

pre = pre + 'HG';}

gr.pre = pre + num;

gr.update();}  

}  

}  

1 ACCEPTED SOLUTION

Slava Savitsky
Giga Sage

I am not sure I understand what you are trying to do with the 'for' loop. Why not just use 'replace' method?


JavaScript String replace() Method


View solution in original post

5 REPLIES 5

Thank you for the heads up:)