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

Sebastian R_
Kilo Sage

You can use GlideRecord.updateMultiple for it (see example). You can edit the encoded query, if you wish to.

var grAsset = new GlideRecord('alm_asset');
grAsset.addEncodedQuery('assigned_toISEMPTY');
grAsset.query();

grAsset.setValue('assigned_to', '');
grAsset.updateMultiple();

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Bowden,

Use fix script for this and as best practice use try catch block for exception handling

Will you be querying for all records where assigned_to is not empty?

Did assigned_to get assigned by mistake to all records or only 25k? What is the table record count?

clearValue();

function clearValue(){

try{

var gr = new GlideRecord('alm_asset');
gr.addQuery('assigned_to', '!=', '');
gr.query();
while(gr.next()){
gr.assigned_to = '';
gr.setWorkflow(false);
gr.update();
}
}
catch(ex){
gs.info('Exception is: ' + ex);
}

}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

just the 25k as the asset record count is 400K+