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.

Mass update through script without calling Business Rules and Events

Chris York
Tera Expert

Is there a way to execute a background script (From Scripts - Background) that updates a record but does not run the business rules on that table?

For example, I have a new field on the Incident table that is a calculated field. However, the calculated value does not get set until the Incident is saved. I want to run a script that does an update on all existing Incident records, but I don't want to trigger any events or business rules as a result of the update.

var gr = new GlideRecord('incident');
gr.query();
while (gr.next()){
gr.update();
}

1 ACCEPTED SOLUTION

Mark Stanger
Giga Sage

You should be able to use the 'setWorkflow' method like this. It says to not run any business rules with the update.



var gr = new GlideRecord('incident');
gr.query();
while (gr.next()){
gr.setWorkflow(false);
gr.update();
}


View solution in original post

5 REPLIES 5

Mark Stanger
Giga Sage

You should be able to use the 'setWorkflow' method like this. It says to not run any business rules with the update.



var gr = new GlideRecord('incident');
gr.query();
while (gr.next()){
gr.setWorkflow(false);
gr.update();
}


Thanks Mark. Worked perfectly.


Hi,
This is certainly very helpful. I'm going to use it in my script.
I am trying to use the following script to perform a mass update on a number of defective INC records. However the script fails to perform the updates. Where am I mistaking?



var gr = new GlideRecord('incident');
gr.addQuery('caller_id', '=', 'NULL');
gr.addQuery('service_offering', '=', 'NULL');
gr.addQuery('assignment_group', '=', 'NULL');
//Use the below line for testing with a smaller set of tickets
//gr.addQuery('number', 'IN', 'INC00zzzzz, INC00zzzzzzz, INC00zzzzzz');
gr.query();
while(gr.next()) {
gs.log('Incident #: ' + gr.number + ' has missing data in mandatory fields');
gr.setWorkflow(false);
gr.caller_id = "something";
gr.service_offering = "something";
gr.assignment_group = "something"
gr.update();
gs.print('Incident #: ' + gr.number + ' has been updated');
gs.log('Incident #: ' + gr.number + ' has been updated');
}
gs.log('TOTAL # of Records processed: ' + gr.getRowCount())


The script looks fine to me. Of course, you'll need to replace "something" with an actual sys_id value to get those reference fields populated with something that matters...either that or use 'gr.caller_id.setDisplayValue('Joe Employee');' to use the display value instead.