Background script to replace old glide_list entries

SC10
Kilo Guru

On my Project table, I have a glide_list field that is referencing a now deleted reference entry (now displays as a sys_id). I've been able to query for all Projects that contain this entry in that field, but I'd like to use a background script to replace this sys ID entry with a different value. Any help is appreciated.

Thank you

1 ACCEPTED SOLUTION

Daniel Draes
ServiceNow Employee
ServiceNow Employee

I would use the ArrayUtil-Script include to parse the current values of your glide_list to an array. Also in ArrayUtil you can do the search to find the proper place of the old value and replace it.

 

Other option - since a glide_list is just a comma-separated string - is a simple replace command. I.e.

 

var my_list_string = gr.some_field + ''; // adding an empty string to force type conversion

my_list_string.replace(old_sys_id, 'New value');

gr.some_field = my_list_string:

gr.update();

View solution in original post

6 REPLIES 6

ashton_sheppard
Giga Contributor

Hi Shane, you can use this and modify as a starting point. I comment out the update section to get a count before committing to an update.

Ashton

var count=0;
var gr = new GlideRecord('sys_user');
gr.addQuery('someField', 'FredL');
gr.query();
while (gr.next()) {
count++;
gr.someField = "newID";
gr.setWorkflow(false);
gr.autoSysFields(false);
gr.update();
}
gs.print (count);

Howdy Ashton! Always nice to hear from you.

I don't think the script will work in my case because it will change the field to only be one entry. My glide_list field has multiple entries on my Projects, and only one of them is the incorrect field and needs to be replaced. I need it to do a replacement of only the incorrect entry, and keep the other valid entries intact.

 

Thanks

Daniel Draes
ServiceNow Employee
ServiceNow Employee

I would use the ArrayUtil-Script include to parse the current values of your glide_list to an array. Also in ArrayUtil you can do the search to find the proper place of the old value and replace it.

 

Other option - since a glide_list is just a comma-separated string - is a simple replace command. I.e.

 

var my_list_string = gr.some_field + ''; // adding an empty string to force type conversion

my_list_string.replace(old_sys_id, 'New value');

gr.some_field = my_list_string:

gr.update();

Tried it as per your code, didn't work, so I tried gr.setValue(x,y); instead, and it also didn't work:

 

var gr = new GlideRecord('pm_project');
gr.addEncodedQuery('u_stakeholder_departmentLIKEcfa078c5dbae3e00adfb591e5e961921');
gr.query();
while (gr.next()){

var my_list_string = gr.u_stakeholder_department + '';

my_list_string.replace("cfa078c5dbae3e00adfb591e5e961921", "Corporate Services");

gr.setValue("u_stakeholder_department",my_list_string);
gr.autoSysFields(false);
gr.update();
gs.print(gr.number);
}