Cancelling Change Workflow

LMHoffman
Kilo Expert

Hi everyone, am working on business rule to cancel change request workflow when a change is made to the Change Type (Out of Process follows one workflow; Standard follows another).

I found an article 'Cancelling Change Workflow' that was posted in May, 2014 (Cancelling Change Workflow) and incorporated what was said in that article into the Business Rule I'm working on. It works except for the following issues -

  • The comment isn't being added to Approval History - I've added Approval History to my Activities and nothing shows up. Don't have much scripting experience but one of my internal developers gave the business rule a look and said it should be working
  • When the change type switches from Out of Process to Standard, the workflow is cancelled and the new workflow is attached. If the Change Type is then switched from Standard to Out of Process, the workflow cancels, but the new workflow isn't attached. Just curious why this is happening, though am not too upset about this one because I can see an never-ending circle.

Appreciate any help with clearing this up!! We're on Fuji.

Used the OOB business rule 'SNC Approval - Reset conditions and modified per the script instructions and added script from 'Cancel Workflows Upon Cancellation'.

find_real_file.png

find_real_file.png

find_real_file.png

Full script -

// these are the conditions under which the change request approvals need to be cancelled 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 = 'Change Type has been changed - marking all existing approvals No Longer Required and creating new approvals';   //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 GlideRecordLock(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 - activating 6/23/16 by LHoffman

                        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);

          //

    }

}

function checkResetConditions() {

    var doReset = false;   //default to no reset

    //add reset conditions here such as:

    //if (current.type.changes())

    //     doReset = true;   //enable the reset

    //

    return doReset;

}

  // Cancel existing workflow - added by LHoffman 06-23-16 from Cancel Workflows Upon

  // Cancellation business rule per community article suggestion titled 'Cancelling

  // Change Workflow'

cancelMyWorkflows();

function cancelMyWorkflows() {

    //get workflow helper

    var workflow = new Workflow();

    //cancel all my workflows

    var numCnxd = workflow.cancel(current);

    if (numCnxd > 1)

          gs.addInfoMessage(numCnxd + " " + gs.getMessage("Workflows for {0} have been cancelled", current.getDisplayValue()));

    else if (numCnxd == 1)

          gs.addInfoMessage("1 " + gs.getMessage("Workflow for {0} has been cancelled", current.getDisplayValue()));

}

17 REPLIES 17

Thank you Michael, I really appreciate your working on this! I made the changes to my script to reflect what you have in bold above. The Approvals show as 'Cancelled', but the comment still isn't making it into the record for me and the workflow isn't cancelling the approval like it was before making the above changes.



find_real_file.png


find_real_file.png



When I switch the script back to the way i had it, i get this on the Approvals section -


find_real_file.png



And this on my Workflow -


find_real_file.png



It's very perplexing to me - and when I moved this into our production environment, it cancels the workflow but doesn't mark the approvals as 'No longer required'.


The field Comments to Requester, that's the Comments field?



In the first screen shot, it says Cancelled at the top.




Screen Shot 2016-07-07 at 9.53.49 AM.png


I think the light just went off in my head.



If you look at the script, there is:


if (checkResetConditions()) {



if: function checkResetConditions() {


has nothing, the whole if is skipped, thus #1 doesn't run.



it then skips down to next fnc: function cancelMyWorkflows() {


which cancels the workflows (approval turns brown on workflow) and the approves are 'No Longer Required' with no comments, because we skipped the if



whereas, the script I'm using with a Reset Condition (current.type.changes()) triggers if, triggers #1, comments get added, workflow is cancelled, tho approval box is green, approver's are cancelled with comments.



Does this make sense?


I tried it your way and that's exactly what happen. No comments, approvers no longer required.


When i tried it with the following, the workflow showed cancelled but the approval box is green, but the comments didn't show up for me.



// these are the conditions under which the change request approvals need to be cancelled 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 = 'This is a test';   //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 GlideRecordLock(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);


              cancelMyWorkflows();


         


         


              //


              // 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);


              //


         


      }


}



function checkResetConditions() {


 


      var doReset = false;   //default to no reset


      //add reset conditions here such as:


      if (current.type.changes())


              doReset = true;   //enable the reset


      //


      return doReset;


}




function cancelMyWorkflows() {


      //get workflow helper


      var workflow = new Workflow();


      //cancel all my workflows


 


      var numCnxd = workflow.cancel(current);


      if (numCnxd > 1)


              gs.addInfoMessage(numCnxd + " " + gs.getMessage("Workflows for {0} have been cancelled", current.getDisplayValue()));


      else if (numCnxd == 1)


              gs.addInfoMessage("1 " + gs.getMessage("Workflow for {0} has been cancelled", current.getDisplayValue()));


}


Did you verify that the Comments for Requestor is the comments field? Just want to make sure it's not a custom field.