Clear our field Value for 25k+ asset records

AndyB5000
Mega Guru

Looking for example scripts for removing the assigned to value from 25k asset records.  It was assigned by mistake and I am looking for a background script that can change the value to NULL.

Thanks.

1 ACCEPTED SOLUTION

You did test with sys_id you mentioned? Because assigned_to is a reference.

Though looking at your script now... updateMultiple will not work, because... you are querying on assigned_to, and then emptying assigned_to? With a while this would be possible, though not with updateMultiple.

Unfortunately, a while is looking at performance really bad. Though querying on the asisgned_to and then emptying this, will just not work with update multiple. So, unfortunately:

var gr = new GlideRecord('alm_asset');
gr.addQuery('assigned_to', 'ce826bf03710200044e0bfc8bcbe5d79');
gr._query();

while(gr._next()) {
	gr.setValue('assigned_to', '');
	gr.setWorkflow(false);
	gr.update();
}

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

7 REPLIES 7

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Something like below might help you. Obviously test with a small set first (therefor I commented the setLimit). Use updateMultiple() for this instead of a while loop and update().

I also added setWorkflow(false) so no business rules will be triggered.

var gr = new GlideRecord('table_name');
gr.addQuery('field_name', 'field_value');
// gr.setLimit(10);
gr._query();

gr.setValue('field_name', '');
gr.setWorkflow(false);
gr.updateMultiple();

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Thanks for the quick response Mark!

 

I have 3 asset records in my DEV instance with the Asset Test user assigned to it.  But when run the query as a background script it does not work.  No errors nothing happens. I setup the script as shown:

var gr = new GlideRecord('alm_asset');
gr.addQuery('assigned_to', 'Asset Test');
// gr.setLimit(10);
gr._query();

gr.setValue('assigned_to', '');
gr.setWorkflow(false);
gr.updateMultiple();

I tried the sys_id of the user record versus the name as well as the user ID versus the name.  I also tried the alm_hardware table also..

 

Hmmm assigned_to is a reference I guess? So addQuery with "Asset Test" looks a bit odd >>> it's not a sys_id. 

So:

gr.addQuery('assigned_to', 'f56abc993928282etc');

+ I removed gr.query, my bad.

Tested below, works on my instance thru background script.

var gr = new GlideRecord('alm_asset');
gr.addQuery('assigned_to', 'ce826bf03710200044e0bfc8bcbe5d79');
// gr.setLimit(10);
//gr._query();

gr.setValue('assigned_to', '');
gr.setWorkflow(false);
gr.updateMultiple();

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

You did test with sys_id you mentioned? Because assigned_to is a reference.

Though looking at your script now... updateMultiple will not work, because... you are querying on assigned_to, and then emptying assigned_to? With a while this would be possible, though not with updateMultiple.

Unfortunately, a while is looking at performance really bad. Though querying on the asisgned_to and then emptying this, will just not work with update multiple. So, unfortunately:

var gr = new GlideRecord('alm_asset');
gr.addQuery('assigned_to', 'ce826bf03710200044e0bfc8bcbe5d79');
gr._query();

while(gr._next()) {
	gr.setValue('assigned_to', '');
	gr.setWorkflow(false);
	gr.update();
}

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn