Add additional comments to multiple incidents

Ken61
Giga Guru

Hello all,

I have a requirement to add additional comments to multiple incident via fix script. Below is my fix script but when I run the script, comments was not added to all the incident. Please assist on what I am doing wrong why the comment are not added to the incidents. Thanks

var gr = new GlideRecord('incident');
gr.addEncodedQuery('active=true^priority=4');
gr.query();
if (gr.next()) {
  gr.comments = 'Please reopen this incident.'; 
  gr.update();
}

 

1 ACCEPTED SOLUTION

Harsh_Deep
Giga Sage
Giga Sage

Hello @Ken61 

 

You are using if condition that's why it's not updating all the incidents

 

Please try below script -

var gr = new GlideRecord('incident');
gr.addEncodedQuery('active=true^priority=4');
gr.query();
while (gr.next()) {
  gr.comments = 'Please reopen this incident.'; 
  gr.update();
}

 

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.

View solution in original post

4 REPLIES 4

OlaN
Giga Sage
Giga Sage

Hi,

In your script you are using an if-statement to write the comment.

This will only process once, if the query returns true, and will write a comment on the first record.

 

If you want to process all the records, change that statement to a while-loop instead.

Some additional thoughts.

You might consider changing/adding to your query, to limit the results, in this case you will write a comment on ALL incidents that are active and has priority 4.

It doesn't seem logical to write a comment to reopen an incident which is already open?

There are no date limitations, so you might get really old incidents back, maybe you should only reopen a subset of all incidents?

Finally, avoid using "gr" as a variable name, use descriptive variable names instead (you will thank yourself later on).

Harsh_Deep
Giga Sage
Giga Sage

Hello @Ken61 

 

You are using if condition that's why it's not updating all the incidents

 

Please try below script -

var gr = new GlideRecord('incident');
gr.addEncodedQuery('active=true^priority=4');
gr.query();
while (gr.next()) {
  gr.comments = 'Please reopen this incident.'; 
  gr.update();
}

 

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.

Ken61
Giga Guru

Thank You @Harsh_Deep . It works now