THURSDAY, FEBRUARY 09, 2012

Reset change request workflow, approvals, and tasks

W

hen implementing the Change management process in Service-now you’ll probably encounter a scenario where your entire change workflow (including all tasks and approvals) needs to be reset.  The first option to consider (assuming you’re using the graphical workflow engine to manage the tasks and approvals) is the Rollback workflow activity.  The rollback activity works great for a lot of scenarios, but what if you don’t have a defined point in the workflow where everything should be rolled back?  What if the rollback (or reset) can happen at any point in time?  There’s not really an easy way to handle this within the workflow itself since you would need to check for a rollback condition at an infinite number of places.

I recently encountered this scenario while working with a client and used the ‘SNC Approval – Reset conditions‘ business rule to solve the problem.  By using a business rule, you can allow the end user (which in this case would typically be a member of the Change Advisory Board) to indicate when a full workflow reset should be done.   Users can force a reset by setting the value of a field on the Change request ticket itself. The ‘SNC Approval – Reset conditions’ business rule gives you 3 different options for how the approval reset should happen.

1 – Cancel all existing approvals and reset
2 – Delete all existing approvals and reset
3 – Leave all existing approvals alone and reset

I chose to use option 1 so that we would have a record of any previous approval activity that had taken place. Depending on the situation and your organization’s audit requirements, option 2 might be a good alternative as well. I can’t think of a scenario where option 3 would be a good thing but it’s there if you need it.

The solution I used was to set up a UI action button that I named ‘Reset change workflow’.  The sole purpose of the UI action is to set some value on the change request ticket to trigger the ‘SNC Approval – Reset conditions’ business rule.  I chose to create a new choice value in the ‘Approval’ field called ‘Reset’.  My business rule checks before any update to see if the value of the ‘Approval’ field is ‘Reset’.  If it is, then the workflow attached to the change request gets reset.  Here’s the step-by-step guide to implement this type of reset functionality…

1Personalize the choices for the ‘Approval’ field on the change request form and add an option called ‘Reset’.

2Create a new UI action with the following parameters:

Name: Reset change workflow
Table: Change request
Client: True
OnClick: setApprovalReset()
Condition: gs.hasRole(‘itil’)
Form button: True
Script:

function setApprovalReset(){
   var con = confirm('Performing this action will reset the associated workflow including all approvals. Are you sure you want to continue?');
   if(con){
      g_form.setValue('approval', 'Reset');
      gsftSubmit(gel('sysverb_update_and_stay'));
   }
   else{
      return con;
   }
}

3Modify the ‘SNC Approval – Reset conditions’ business rule as follows:

Name: SNC Approval – Reset conditions
Table: Change request
Active: True
Condition: current.approval.changesTo(‘Reset’) && gs.hasRole(‘itil’)
Script:

// these are the conditions under which the change request approvals need to be canceled and reset
// steps to activate
// 1. enable this business rule
// 2. add some comments so the reset will be noted in the approval history
// 3. uncomment the code for restart or reset based on your requirements
// 4. define the reset condition in checkResetConditions function below
// 5. you must set doReset once you capture the change(s) you are interested in

var comment = 'Approval reset by ' + gs.getUserDisplayName(); //written to the approval_history
if (checkResetConditions()) {
   // create a global variable lock on the current record
   // this will prevent triggering a second reset while the first reset is still in progress
   // lock will be release in a late running business rule called 'Workflow Release Lock'
   chg_wf_lock = new Packages.com.glide.script.RecordLock(current);
   chg_wf_lock.setSpinWait(50); //wait for lock
   if (chg_wf_lock.get()) {
      gs.print('SNC Approval conditions business rule is locking the ' + current.getDisplayValue() + ' during the workflow reset');

      // The following options are available for resetting the approvals:
      //
      // 1. Mark all existing approvals for the change as 'cancelled' and restart the workflow to create new approvals
      new WorkflowApprovalUtils().cancelAll(current, comment);
      new Workflow().restartWorkflow(current);
      //
      // 2. Delete all of the existing approvals for the change and restart the workflow to create new approvals
      // new WorkflowApprovalUtils().reset(current, comment);
      // gs.addInfoMessage('Workflow has been reset since key fields have been modified');
      //
      // 3. Leave the approvals for the change in their current state and restart the workflow.
      // (so that any new approvals that are required will be created)
      // if (comment)
      //    current.setDisplayValue('approval_history', comment);
      //    new Workflow().restartWorkflow(current, true);
      //

   }

   //Use this section to reset any necessary information on the Change request
   current.work_notes = 'Change approvals and workflow reset due to material change by ' + gs.getUserDisplayName();
   current.approval = 'not requested';

   //Use this section to reset any necessary information on any associated Change tasks
   var tsk = new GlideRecord('change_task');
   tsk.addQuery('change_request', current.sys_id);
   tsk.query();
   while(tsk.next()){
      tsk.work_notes = 'Change request workflow reset.';
      tsk.update();
   }
   gs.addInfoMessage('Change approvals and workflow reset for ' + current.number + '.');
}

function checkResetConditions() {
   var doReset = false; //default to no reset
   //add reset conditions here such as:
   //if (current.assigned_to.changes())
   doReset = true; //enable the reset

   return doReset;
}

Comments

Posted On
Dec 29, 2009
Posted By
alli

seems, all of your posts are connected to my open tickets in the hi server :)

cheers

Posted On
May 12, 2011
Posted By
John

I need to be able to reset approvals on a non-change mgmt workflow. Can I simply duplicate this process, specifying the table on which the workflow runs?

Posted On
May 12, 2011
Posted By
Mark Stanger

Pretty close. The ‘change_task’ section towards the bottom of the ‘SNC Approval – Reset conditions’ business rule would need to be modified since it’s change-specific, but the rest should be identical.

Posted On
Jun 27, 2011
Posted By
Chris

Thanks this is great information. Wanted to mention the link in 2nd paragraph for ‘SNC Approval – Reset conditions‘ appears to be broken. You did include the script later on but thought it was worth mentioning.

Cheers and thanks for another great article!

Posted On
Jun 27, 2011
Posted By
Mark Stanger

Thank you! Looks like the wiki article changed a bit. I’ve fixed the link to point to the new location.

Posted On
Jul 01, 2011
Posted By
Michael S

Excellent ideas here. Thanks so much for the contribution Mark. I am a newbie to this community, so my apoligies for any stupid questions.

Will this reset approach work when the reason why the change record needs to be reset is that the wrong change workflow was selected.

Let me explain. Impact and Risk are used to automatically set the our change workflow. We have 3 change workflows. Routine, Standard and Emergency. Depending on the Impact and Risk choice, one of these 3 workflows are set. What happens is that after the change is submitted and the workflow is kicked off, someone re-evaluates the impact and risk and they want to change it. Thus changing the workflow from say like standard to routine.

I have experimented with using the setApprovalReset function approach but I can’t get it to reset and use a different workflow. So is it possible or do I need to go in a different direct altogether?

Posted On
Jul 02, 2011
Posted By
Mark Stanger

Hey Michael,
I don’t mind stupid questions as long as you’re okay with stupid answers :) . This is a good question though. I think you’ll want to go in a little bit different direction on this. You probably want to cancel the entire workflow and then set up your other workflows so that they will bind when the conditions change. You can find some examples here…
http://www.servicenowguru.com/scripting/business-…

Posted On
Dec 08, 2011
Posted By
Catherine Crigger

Hi Mark,

I have followed your instructions here for setting up this Reset Change Workflow and have run into a couple of snags.

1. The Reset Change Workflow button now appears on the Change Form, but what I would like to do is limit it to be seen by only a specific GROUP of users. Can you help me with what changes I need to make.

2. When I copied the script I am getting this error
JavaScript parse error at line (1) column (28) problem = illegal character

Can you point me in the right direction on how to fix this?

Thank you for your help.

Catherine

Posted On
Dec 08, 2011
Posted By
Mark Stanger

Hey Catherine. You can set the ‘Condition’ field on the UI action to restrict access. You can restrict access to a particular group by using a line like this for the condition…

gs.getUser().isMemberOf('YOUR_GROUP_NAME_HERE');

As for the javascript error, it’s probably caused by copying the code and trying to paste it directly into a script field with the syntax highlighter on. You should temporarily disable the syntax highlighter for the script field by clicking on the button next to the script field label, then paste the code in. That should prevent you from seeing these types of errors when you copy/paste code from the web. Another method would be to copy the code and paste first into notepad or something similar to strip the web formatting.

Posted On
Dec 08, 2011
Posted By
Catherine Crigger

Mark,

Just to be a little bit more clear on the JavaScript Error it is actually in this condition

current.approval.changesTo(‘Reset’) && gs.hasRole(‘itil’)

Any ideas where I should look?

Thanks
Catherine

Posted On
Dec 09, 2011
Posted By
Mark Stanger

Simplest way to find out is just to remove each piece of the condition until the error goes away, then you’ll know what part contains the error. My best guess is that it’s still a copy/paste error. Since it’s small, you might just try typing the condition directly and see if that helps.

Posted On
Dec 13, 2011
Posted By
David Martin

Hi Mark

I have used this numerous times, mainly using scenario 1, however on this occasion I need the reset to reset the workflow but not create any approvals straight away. I want them to be created when they go through the workflow again.

Any ideas how I could achieve this, I do not want to delete them, just keep them, probably cancel them or keep the states as they are.

Thanks
Dave

Posted On
Dec 13, 2011
Posted By
Mark Stanger

Hey Dave,

I think for that scenario it might be more effective just to use the standard workflow rollback activity. The way to handle that in the workflow is to set up a ‘Wait for’ workflow activity, followed by a rollback, that spans the entire duration that a rollback could potentially occur. The ‘Wait for’ could wait for a specific state that would be set by your UI action.

Posted On
Dec 14, 2011
Posted By
David Martin

Thanks Mark, I have multiple points of when the workflow could be reset, so was hoping to have just 1. Thinking about it, in this situation it may be better to use the rollback functionality.

Cheers
Dave

Leave a Reply


Notify me of followup comments via e-mail. You can also subscribe without commenting.

Latest Comments

  • Aric: Finally figured out what I was doing wrong, incase someone else wants to do this. Mark is correct about needing...
  • Mark Stanger: You’ll probably need at least 3 total ACLs then. You’ll need one...
  • Mark Stanger: Hey Paul, I assume you’re talking about ‘task_ci’, not ‘task_group. If so, the...
  • ND: Hi Mark, This is cool. How can I do same at the form level. I have created a slushbucket variable and I want to...