Reset a workflow Change Request - Normal in dev instance

frappe
Kilo Expert

I want to create a UI action for the user to unsubmit their request for approval.   I dug around and found a few references but am stuck.

The code below seems to be working as far as removing the approvals.   The problem I'm having is that the workflow which is the default dev instance workflow named Change Request - Normal is not resetting and there's no longer an reference link to "Show workflow".     My assumption would be that the workflow was reset and the state of the change would go back to New, currently is stays in Assess.

var comment = 'Change Owner decided to un-request approval';   //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('Locking the ' + current.getDisplayValue() + ' during the workflow reset');

          //Delete all of the existing approvals for the change and restart the workflow to create new approvals

          // NOTE:   the code below came from SNC — Approval Reset Conditions so if it's not accurate you may want to update in a future release.

            // NOTE:   I also tried adding   "new Workflow().restartWorkflow(current);" after the first line below as it seemed missing but that didn't work either.

       

                          new WorkflowApprovalUtils().reset(current, comment);

                          gs.addInfoMessage('Workflow has been reset.   All approvals have been deleted.');

    }

   

}

function checkResetConditions() {

    var doReset = true;  

          doReset = true;   //enable the reset

    //

    return doReset;

}

1 ACCEPTED SOLUTION

Hi Kristin,



I'd go with something like this *untested* UI action script:



current.comments = 'Change Owner decided to un-request approval';


current.state = 1; // update the 1 to your desired state;


current.update();


//mark all existing approvals for the change as 'cancelled' and restart the workflow to create new approvals


//where current is a task record with a workflow context


new WorkflowApprovalUtils().cancelAll(current, comment);


new Workflow().restartWorkflow(current);


gs.addInfoMessage('Change request reset');


action.setRedirectURL(current);



//Sorry about the funky coloring - copy/paste


View solution in original post

12 REPLIES 12

Chuck Tomasi
Tera Patron

Hi Kristin,



There's a lot of extra (dangerous) stuff in that script you don't need to be concerned with. You should look at using :



new Workflow().restartWorkflow(current, false);



You'll also want to update



current.state = 1; // Adjust to your default state as needed.



Reference: Workflow Script - ServiceNow Wiki


Ok, thanks.   Remember I got the dangerous stuff from the out of box SNC - Approval Reset Conditions in the out of box dev instance so if it's dangerous you might want to make it undangerous.         Can you send me the suggested script?   Not sure what dangerous stuff to remove ... I'm assuming it's the lock?


Hi Kristin,



I'd go with something like this *untested* UI action script:



current.comments = 'Change Owner decided to un-request approval';


current.state = 1; // update the 1 to your desired state;


current.update();


//mark all existing approvals for the change as 'cancelled' and restart the workflow to create new approvals


//where current is a task record with a workflow context


new WorkflowApprovalUtils().cancelAll(current, comment);


new Workflow().restartWorkflow(current);


gs.addInfoMessage('Change request reset');


action.setRedirectURL(current);



//Sorry about the funky coloring - copy/paste


This worked !!! And was much easier to understand, thank you very much.