Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Update an attribute of records in an array

Lemajeur
Mega Expert

Hello, I have an array that has a bunch of sys ids of my query.  Let’s us say that my array has a list of open incidents/ incident task and is defined as follow

 

var allIncidents = [];

 

assuming that my records are are in it already , how can I change the attribute for all records (sys_id) for example to open , open = 4

 

i am doing this:

allIncidents.state = 4;

 

but it it seems to not work

1 ACCEPTED SOLUTION

Dubz
Mega Sage

You will need to use those sys_ids to access the incident table, find the records and then update them. here are some options:

//iterate throught the array and update each record individually:

var allIncidents = [];

var gr = new GlideRecord('incident');
for(var i=0; i< allIncidents.length; i++){
if(gr.get(allIncidents[i]){
gr.state = 4;
gr.update();
}
}


//get all records in a glide query and use update multiple:

var allIncidents = [];

var gr = new GlideRecord('incident');
gr.addEncodedQuery('sys_idIN' + allIncidents);
gr.query();
gr.setValue('state', 4);
gr.updateMultiple();

 

EDIT: incident and incident_task are different tables so you won't be able to update all records in an array that contains sys_id's for each table. Create 2 arrays, one with incidents and the other with incident_tasks

View solution in original post

3 REPLIES 3

Dubz
Mega Sage

You will need to use those sys_ids to access the incident table, find the records and then update them. here are some options:

//iterate throught the array and update each record individually:

var allIncidents = [];

var gr = new GlideRecord('incident');
for(var i=0; i< allIncidents.length; i++){
if(gr.get(allIncidents[i]){
gr.state = 4;
gr.update();
}
}


//get all records in a glide query and use update multiple:

var allIncidents = [];

var gr = new GlideRecord('incident');
gr.addEncodedQuery('sys_idIN' + allIncidents);
gr.query();
gr.setValue('state', 4);
gr.updateMultiple();

 

EDIT: incident and incident_task are different tables so you won't be able to update all records in an array that contains sys_id's for each table. Create 2 arrays, one with incidents and the other with incident_tasks

Hi Leandre,

Did this answer your question? If your issue is resolved please mark my answer correct.

Cheers

Dave

thank you David !