The CreatorCon Call for Content is officially open! Get started here.

Update multiple records with background script

Dawid2
Giga Guru

I need to update couple of the records with the help of the background script. The field I need to update is a free text field, which = to value NAAM. I need to update it so it says NAM.

Here's the script I'm using but it's not working - anyone knows why?

var rg = new GlideRecord('incident');
rg.addQuery("u_alias1", "=", "NAAM");
rg.query();
while(rg.next){
rg.u_alias1="NAM";
rg.update();
}

1 ACCEPTED SOLUTION

Martin Ivanov
Giga Sage
Giga Sage

try with

var rg = new GlideRecord('incident');
rg.addQuery("u_alias1", "=", "NAAM");
rg.query();
while(rg.next()){
rg.setValue('u_alias1','NAM');
rg.update();
}


Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024

View solution in original post

6 REPLIES 6

Community Alums
Not applicable

Hi Rohila

 

Never directly assign a value to a variable and call multipleUpdate()!

That will cause all records of the table to have the value changed whatever the query!!

Always use .setValue() together with updateMultiple()!

 

Please fix your comment or remove it before someone, for some reason, decides to use your way of updating the records and actually destroy data in the table!

 

Best regards

Nuno Agapito

Thank You Nuno, I modified the code.


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP