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.update() not working

Manikandan2
Kilo Expert

Hi,

gr = new GlideRecord('incident');
gr.addQuery('stateNOT IN6,7,8');
gr.query();
while (gr.next()) {
gr.description = gr.description + '.';
gr.update();
}

The above is a small code i am writing to update all open incidents(anything except Resolved, Closed, Cancelled) state with a " . " in the description

When i run this query in the scripts background it is randomly updating only few tickets and it is not updating all tickets.

I am not sure what wrong i am doing here. What change should i make in this query to update all the incidents. 

16 REPLIES 16

Vismit Ambre
Giga Guru

 

 

This is the result I got. I had limited the result set to 4 for testing.

find_real_file.png

var incidentGR = new GlideRecord('incident');
var oldDescription = '';
incidentGR.addEncodedQuery('stateNOT IN6,7,8');
incidentGR.setLimit(4);
incidentGR.query();
while(incidentGR.next())
 {
  oldDescription = incidentGR.description;
  incidentGR.description = incidentGR.description + '.';
  incidentGR.update();
  gs.log('Incident ' + incidentGR.number + ' has been updated. Old Description: ' + oldDescription + '. New Description: ' + incidentGR.description);
 }

Brent Llewellyn
Mega Guru

Hi Manikandan,

 

The issue seems to be in your addQuery function. addQuery expects the following parameters: name, operator(optional), and value. In your script you are passing the query string. 

 

To use addQuery in your case it would look like this

gr.addQuery('state','NOT IN','6,7,8');

 

To use it in the query format you have you would need to use an addEncodedQuery which expects the actual query string which can be grabbed from a list view by right clicking the filter and closing "Copy query". 

To use addEncodedQuery in your case it would look like this

gr.addEncodedQuery('stateNOT IN6,7,8');

A working solution using addEncodedQuery would look like this

var gr = new GlideRecord('incident');
gr.addEncodedQuery('stateNOT IN6,7,8');
gr.query();
while (gr.next()) {
    gr.description = gr.description + '.';
    gr.update();
}

 

Thanks, 

Brent

 

If this helps please mark as helpful or correct so that others may benefit from this information as well.