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

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

The problem was that you were missing the brackets after the .next() method.

Please makr Correct and Helpful. Thanks


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

Himanshu Gupta1
Mega Guru

Hi 

 

Please do you setworkflow(false) and autosysfield(false) as well so that it wil not trigger or update user that you doing something at backend


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

Please mark it helpful and correct if your query get resolved

Best Regards

Himanshu Gupta

Voona Rohila
Mega Patron
Mega Patron

Hi Dawid

Try this

var rg = new GlideRecord('incident');
rg.addQuery("u_alias1", "=", "NAAM");
rg.query();
rg.setValue('u_alias1',"NAM");
rg.updateMultiple(); 

https://community.servicenow.com/community?id=community_article&sys_id=53e36473dbd244502be0a851ca961...

 

you can also achieve this with no-coding using update-selected/update all option.

https://docs.servicenow.com/bundle/rome-platform-user-interface/page/use/using-lists/task/t_EditMult...


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