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,



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


Thanks.   That gets rid of those messages.


Is it actually cancelling the workflow?


From the wiki - Enables or disables the running of business rules that


might normally be triggered by subsequent actions. If the parameter is set


to false, an insert/update will not be audited. Auditing only happens when


the parameter is set to true for a GlideRecord operation



On Nov 17, 2017 10:45 AM, "jmiskey" <community-no-reply@servicenow.com>


Please forgive me, I am a bit of newbie here when it comes to workflows, but what exactly does that mean in terms of the workflow?   Is it cancelling it, or not cancelling, but just not returning the messages regarding it?