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.

gr.initialize() in loop to update records

rahulyamgar
Tera Guru

Hello friends,

 

I came across a scenario in which I need to update multiple incident records and came up with below script:

 

 

var gr=new GlideRecord('incident');
var a=['INC0010002','INC0010003','INC0010004','INC0010005','INC0010006','INC0010007'];
for(var i in a)
{
gr.addQuery('number',a[i]);
gr.query();
gr.next();
gs.info(gr.getEncodedQuery());
gr.setValue('short_description','Test');
gr.update();
}

 

Above, gs.info() prints something like below:

 

*** Script: number=INC0010002
*** Script: number=INC0010002^number=INC0010003
*** Script: number=INC0010002^number=INC0010003^number=INC0010004
*** Script: number=INC0010002^number=INC0010003^number=INC0010004^number=INC0010005
*** Script: number=INC0010002^number=INC0010003^number=INC0010004^number=INC0010005^number=INC0010006
*** Script: number=INC0010002^number=INC0010003^number=INC0010004^number=INC0010005^number=INC0010006^number=INC0010007

 

I dont want this to happen. In order to eliminate this and have proper query formed, i did following: (added gr.initialize in loop.)

 

var gr=new GlideRecord('incident');
var a=['INC0010002','INC0010003','INC0010004','INC0010005','INC0010006','INC0010007'];
for(var i in a)
{
gr.initialize();
gr.addQuery('number',a[i]);
gr.query();
gr.next();
gs.info(gr.getEncodedQuery());
gr.setValue('short_description','Test');
gr.update();
}

 

After which I got following output:

 

*** Script: number=INC0010002
*** Script: number=INC0010003
*** Script: number=INC0010004
*** Script: number=INC0010005
*** Script: number=INC0010006
*** Script: number=INC0010007

 

Did i do the right thing? Shouldn`t have I used initialize()? What would you do?

 

Thanks in advance.
R

 

3 REPLIES 3

AshishKM
Kilo Patron
Kilo Patron

Hi @rahulyamgar , 

you dont need to use gr.initialize() for updating the existing record.

gr.initialize() used for creating/inserting a new record. 

do not use the glide query in loop like this instead provide all INC number in addQuery() at once and update the record.

 

-Thanks,

AshishKM

 

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

maroon_byte
Mega Sage

Why array? Any issue using below:

var grIncident = new GlideRecord('incident');
grIncident.addQuery("number","IN","INC0010002,INC0010003,INC0010004,INC0010005,INC0010006,INC0010007");
grIncident.orderByDesc('sys_updated_on');
grIncident.query();
while (grIncident.next()) {
    grIncident.setValue('short_description','Test');
    grIncident.update();   
}

Fabio Fernandes
Tera Contributor

if it is to update always the same field, you could use the following script:

 

var gr = new GlideRecord('incident')
gr.addQuery('number','IN', 'INC0010002,INC0010003,INC0010004,INC0010005,INC0010006,INC0010007');
gr.query();
gr.setValue('short_description', 'test');
gr.updateMultiple(); 

 

 

this will update all the incidents in that query