- 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-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-03-2015 05:38 AM
You might also want to use autoSysFields(false) to avoid changing the values of "Updated" and "Updated by" fields.
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 02:52 AM
Slava - this were a couple of very helpful tips man, thanks.
So I did a second version of my background script using the advise and I managed to did exactly what I needed. The script I used was:
addPrefix();
function addPrefix() {
var gr = new GlideRecord('change_task');
gr.query();
while (gr.next()) {
gr.autoSysField(false); // so that the records don't have system updates
gr.setWorkflow(false); // so no business rules are run
gr.number = gr.number.replace("CTASK","CHGTASK");
gr.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-04-2015 03:47 AM
Hi Dimitar,
I am glad to hear you got the script working. Yet an "s" seems to be missing in gr.autoSysFields(false) statement, so updates to system fields may not have been supressed. You may want to double check that.
Blog: https://sys.properties | Telegram: https://t.me/sys_properties | LinkedIn: https://www.linkedin.com/in/slava-savitsky/