Not able to change Incident state from In-progress to New via Background script/Fix Script

Kiruthika J
Tera Contributor

Hi All,

I want to change Incident state from In-progress(2) to New(1) via background script/ Fix script.

var inc = new GlideRecord('incident');
inc.query();
while (inc.next()) {
var sta = inc.getValue('state');
if ((sta == '2')) {
inc.state = '1' ;
inc.update();
}
}

Getting Below Error. For other states it's working fine.

find_real_file.png

1 ACCEPTED SOLUTION

Mohit Kaushik
Mega Sage
Mega Sage

Hi Kiruthika,

Try to add setWorkflow(false) in your script as shown below:

var inc = new GlideRecord('incident');
inc.query();
while (inc.next()) {
var sta = inc.getValue('state');
if ((sta == '2')) {
inc.state = '1' ;
inc.setWorkflow(false);
inc.update();
}
}

 

Please mark this as correct and helpful if it resolved the query or lead you in right direction.

Thanks,
Mohit Kaushik
Community Rising Star 2022

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

View solution in original post

6 REPLIES 6

Hi Kiruthika,

The serWorkflow() method accepts one argument: a boolean true/false value. This argument will determine whether business rules should be triggered by any database actions performed by your GlideRecord script. For example, if you make a change and call the update() method, calling setWorkflow() and passing in false will prevent any business rules that would normally be triggered by that update from running.

Below link can also give you more info.

GlideRecord - Scoped | ServiceNow Developers

 

Please mark this as correct and helpful if it resolved the query or lead you in right direction.

Thanks,
Mohit Kaushik
Community Rising Star 2022

 

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

Musab Rasheed
Tera Sage
Tera Sage

Hi,

Try below, Also please create fix script for such things instead of Background script and also run fix script in background.

var count = 0;
var inc = new GlideRecord('incident');
inc.addQuery('state' , '2');
inc.query();
while (inc.next()) {
count++;
inc.state = '1' ;
inc.setWorkflow(false);
inc.update();
}
}
gs.print("Total number of Incidents changed is " +count);
Please hit like and mark my response as correct if that helps
Regards,
Musab