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

vinothkumar
Tera Guru

Adding one point, clearing the name to empty will also make empty all the reference record that was created earlier by this inactive user   like requested for, etc.,.


venkatiyer1
Giga Guru

Hi Joe,



That is one way and it looks fine. Alternatively, you can write to a new custom table as well before updating.


But for now if the above works, then it is good enough. Is u_comments a journal field? If it is a journal field, even better.



Thanks


Venkat


venkatiyer1
Giga Guru

I agree with vinoth. Skipped my notice, i believe you should omit that line..


ia.user_name = ''


I agree with vinoth. Skipped my notice, i believe you should omit that line..


ia.user_name = ''


But that is the whole goal of my process, to remove the value in the user_name field!


I agree Joe, please ignore my above comment.