Cancel a workflow in a background script

jmiskey
Kilo Sage

So, we have been having some issues when importing new users into ServiceNow.   Specifically, some records fail because the value we are importing for the User_Name field is sometimes already used (by old, inactive records), and the field is a Unique field.   So, what we would like to do is to remove the User_Name values for all Inactive users in ServiceNow.   So we created the following background script to do that:

var recs = 0;

var ia = new GlideRecord('sys_user');

        ia.addQuery('active','=','false');

        ia.addQuery('user_name','!=','');

//         ia.addQuery('last_name','=','Wilson');

        ia.query();

recs = recs + ia.getRowCount();

while (ia.next()) {

      ia.user_name = '';

      ia.update();

}

gs.print(recs + " User_Names removed!");

So, the script seems to do what we want, but with one caveat.   It seems to cancel some workflow activities, but not the actual workflow running on the record.   So we get messages like this:

completed Begin(81a6a52a0fb03240e3b522d8b1050e90): event=update

completed Wait for term date(45a6ade60fb03240e3b522d8b1050e62): event=execute

completed Check for "assigned to" licenses and unassign(cda6ade60fb03240e3b522d8b1050e65): event=execute

completed Deactivate User(c5a6ade60fb03240e3b522d8b1050e65): event=execute

completed End(cda6a52a0fb03240e3b522d8b1050e8f): event=execute

completed Begin(81a6a52a0fb03240e3b522d8b1050e90): event=update

completed Wait for term date(45a6ade60fb03240e3b522d8b1050e62): event=execute

completed Check for "assigned to" licenses and unassign(cda6ade60fb03240e3b522d8b1050e65): event=execute

completed Deactivate User(c5a6ade60fb03240e3b522d8b1050e65): event=execute

completed End(cda6a52a0fb03240e3b522d8b1050e8f): event=execute

...

Is there any way to amend our script so that it does not do this (cancel some of the workflow activities)?  

Or perhaps make it instead cancel the entire workflow?

(Cancelling some of the activities but not the entire workflow seems like it might cause problems).

Thanks

1 ACCEPTED SOLUTION

venkatiyer1
Giga Guru

Hi Joe,



ia.setWorkflow(false) before update should avoid triggering the workflow.


View solution in original post

14 REPLIES 14

venkatiyer1
Giga Guru

Hi joe,



We would like to essentially update the record here without triggering the workflows. So the above code essentially updates the record but skips the running of business rules that are tied to the record which in turn does not allow the triggering of the workflows. There is no cancellation, the workflows are not triggered. It gets triggered by business rules associated with update or insert of a record.


Thank you very much for your assistance!


Venkat,



Just one follow-up.   It looks like disabling those workflows had one unintended consequence.   It basically prevents the history Activity record from being written (that shows the field being changed).   We kind of like that being there, as we can refer to it, if we ever want to see what someone's value was before it was removed.



Is there any way to amend my background script to write that Activity history record?


venkatiyer1
Giga Guru

Hi Joe,



in SetWorkflow, Auditing only happens when the parameter is set to true for a GlideRecord operation.


i.e. when set to True auditing happens otherwise not.




Now fields like sys_updated_by, sys_updated_on, sys_mod_count, sys_created_by, and sys_created_on should get updated with the update operation. it is just the audit table would not get updated.




There is a functionality called autoSysFields(boolean e) which can even turn off the update to sys_updated_by, sys_updated_on, sys_mod_count, sys_created_by, and sys_created_on by doing something like   inc.autoSysFields(false);


So I am not sure that helps solve the problem we have.   We want to capture the value of the User ID in a history record written to the member, but don't want the workflows to run.



I did some playing around, and I think I figured out a way to update a comments field we have, which should be good enough (as there will be visible details of that the field used to be before being removed).



Do you see any issues with this code?


var recs = 0;


var ia = new
GlideRecord('sys_user');


      ia.addQuery('active','=','false');


      ia.addQuery('user_name','!=','');


//       ia.addQuery('last_name','=','Wilson');


      ia.query();


recs = recs + ia.getRowCount();


while (ia.next()) {


      var userid=ia.user_name;


      var str='User ID ' + userid + ' removed';


      ia.user_name = '';


      ia.u_comments=str;


      ia.setWorkflow(false);   //prevent workflows from kicking off


      ia.update();


}


      gs.print(recs + " User IDs removed!");