Restart CHG Workflow with UI Action

shane_davis
Tera Expert

All,

        We have the business rule in the Wiki article below working.   It is set to "Delete all of the existing approvals for the change and restart the workflow to create new approvals" when the assigned_to changes.

Reset Workflow Based on Changes to Record - ServiceNow Wiki

       

        Our goal is to have a UI Action called "Restart Workflow" on the Change Request table that will delete all of the existing approvals for the change and restart the workflow when the UI Action is clicked.   The conditions and code we have are below.   It is deleting the approvals and giving the message printed, but is not restarting the workflow.

UI Action

Table: Change Request

Action name: blank

Show update: true

Client: unchecked

Condition:   current.state != 3 && current.state != 5 && gs.hasRole('itil')           //Does not equal Closed Complete and does not equal Cancelled and user is itil

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

          //

          // 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 = true;   //default to no reset

    //add reset conditions here such as:

    //if (current.assigned_to.changes())

          //doReset = true;   //enable the reset

    //

    return doReset;

}

2 REPLIES 2

Gurpreet07
Mega Sage

You need to uncomment line       new Workflow().restartWorkflow(current, true);     to restart the workflow.


Gurpreet,



        Thank you for your reply.   I got completely sidetracked today and have not tested, but I will test soon and update you.   Thanks again.



Shane