- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-03-2015 05:31 AM
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();}
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-03-2015 05:36 AM
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
Blog: https://sys.properties | Telegram: https://t.me/sys_properties | LinkedIn: https://www.linkedin.com/in/slava-savitsky/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-04-2015 05:08 AM
Thank you for the heads up:)