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

PS:   I spoke a little too soon but for what it's worth this code worked if I wanted to delete all requests on the change and reset the status to New:



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


current.state = -5; // set state back to New


current.update();


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


new WorkflowApprovalUtils().reset(current);


new Workflow().restartWorkflow(current);


gs.addInfoMessage('Change request reset');


action.setRedirectURL(current);


Hi Kristin,



Do we need to make some changes or is that your final version of what I gave you (adapted to meet your requirements)?


  I am not a developer these days but I'm learning fast ... so not sure



Original line:


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


In my version I changed cancelAll to reset to meet my requirements but that did not work either until until I also removed comment next to the word current.


new WorkflowApprovalUtils().reset(current);







Hey Chuck ,

 

Even i have the same requirement and i used the above Ui action script but it did not run . 

 

Any modifications or suggestions please ?

 

Thanks in advance .

 

Regards,

Rishi 

 

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

HI Kristin,



Please check 3.18 here for restart workflow


Workflow Script - ServiceNow Wiki