gr.initialize() in loop to update records
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2024 06:19 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2024 06:27 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2024 06:29 AM
Why array? Any issue using below:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2024 06:44 AM
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