Reset Workflow with a UI Action

dianemiro
Kilo Sage

Hi All,

Anyone who has an idea how to reset or restart a workflow with a UI Action? Basically, I have my UI Action made and here is my configuration:

find_real_file.png

With this script:

function back2Draft() {

  //Set the 'State' values to 'Review', and display mandatory fields

  g_form.setValue('state',1);

  //Call the UI Action and skip the 'onclick' function

  gsftSubmit(null, g_form.getFormElement(), 'demand.back2Draft'); //MUST call the 'Action name' set in this UI Action

}

if (typeof window == 'undefined')

  ServerBack2Draft();

function ServerBack2Draft() {

  var objctask = new GlideRecord("change_task");

  objctask.addQuery('u_demand',current.sys_id);

  objctask.query();

  while(objctask.next()){

  objctask.state = 7;

  objctask.update();

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

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

  current.state = 1;

  current.update();

  action.setRedirectURL(current);

  }

}

The Demand State is not changing and the Change Tasks only Closed Skipped the 1st task.

find_real_file.png

Many thanks!

1 ACCEPTED SOLUTION

preddy
Kilo Guru

Hi Diane,



Can you see the below code I was updated and added the code to delete task.



cancelWorkflow();


deleteTasks();



current.state = 6;


gs.addInfoMessage('Change ' + current.number + ' has been reset. Any open tasks have been cancelled and the Change will need to go through the Approval process again, if required.');


current.update();



function cancelWorkflow(){


new WorkflowApprovalUtils().cancelAll(current,'Workflow has been restarted, new approval will be needed.');


var wf = new Workflow();


wf.deleteWorkflow(current);


}



function deleteTasks(){


// Delete any running change tasks


var cTask = new GlideRecord('change_task');


cTask.addQuery('change_request',current.sys_id);


cTask.query();


while (cTask.next()){      


      cTask.deleteRecord();      


}


}


}


Please let me know if my comment was helpful and mark it accordingly (like, helpful, correct) so that others can be helped.


View solution in original post

7 REPLIES 7

anjalichoudhary
Kilo Guru

Hi Diane,



This below link will help you to find the exact code to restart workflow in UI Action:



Restart Workflow in requested item by UI action



Hope it helps!!



Regards,


Anjali


preddy
Kilo Guru

Hi Diane,



The below code is helpful for you to restart your workflow. AT that point first you have to cancel your running workflow and again you need to restart.



cancelWorkflow();



function cancelWorkflow(){


// Cancel running workflows.


new WorkflowApprovalUtils().cancelAll(current,'Workflow has been restarted, new approval will be needed.');


var wf = new Workflow();


wf.deleteWorkflow(current);


}



Please let me know if my comment was helpful and mark it accordingly (like, helpful, correct) so that others can be helped.


dianemiro
Kilo Sage

Hi All,



Still no progress about in resolving this issue. Do you have an idea on how can I restart the workflow and erase or remove all tasks related to it?



Many thanks!!!