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

Michael Fry1
Kilo Patron

The business rule is on the Change Request table. So the comment should show on the change request, not the approval request.



As for the workflow issue, workflows default is to attach once to a ticket. There is a checkbox on workflows to Run mulitple, which will allow the workflow to attach whenever the conditions are true.


Hi Mike, thanks for responding!



I'll look at the workflows checkbox - didn't know/think about that



For the comment, i see what you're saying though it's not showing up in my Activity as a comment either - any suggestions?



find_real_file.png




Activity Configuration -


find_real_file.png


The var comment defines the variable, where is it called? Your screen shot of your business rule only shows 10 lines, is there more?


Full script from the business rule -



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


}