Cancel workflow with UI Action

palmen
Tera Guru

I've got a requirement to be able to cancel a workflow at any given time during the time the workflow is active.

I found this thread which gave me more or less all answers on how to do this.

UI Action Cancel Workflow

What I need help with is that all open tasks in the workflow should be closed with a specific close note, but the ticket shouldn't get the same close note (it should still be blank).

My current code is as follows

function cancel(){  

    if(confirm('Are you sure you want to cancel the New Supplier process?')){  

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

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

        return true;   //Abort submission  

    }  

    return false;  

}  

 

 

// Server side code  

if(typeof window == 'undefined')  

    cancelNewSupplier();  

 

 

function cancelNewSupplier(){  

  current.active=false;  

  current.state=4;   //Closed Incomplete

  new Workflow().cancel(current);

  action.setRedirectURL(current);  

  current.update();  

  gs.addInfoMessage('The New Supplier Process has been canceled.');  

}

The function cancelNewSupplier() set the same state to all open tasks and the ticket which is OK, but the close code should only go to the open tasks.

How do I achieve this?

I tried with the following code but it only set the close notes for the ticket

current.close_notes='New Supplier Process were canceled by SCM';
1 ACCEPTED SOLUTION

This works fine, it set everything to the open tasks and no more tasks are being created.


But there is another thing that happened now, or should I say not happen. I do have a script that copy close notes from the task to the ticket and that script isn't executing anymore for the tasks being closed.



I guess I'll just have to make a decision which code to use, either the one you suggested (pasted below) or the one I used before which wasn't optimal (from my post Sep 3, 2015 2:33 PM)



function cancel(){


  if(confirm('Are you sure you want to cancel the New Supplier process?')){


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


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


  return true;   //Abort submission


  }


  return false;


}



// Server side code


if(typeof window == 'undefined')


  cancelNewSupplier();



function cancelNewSupplier(){


  //Close all active supplier tasks, set work notes and cancel the workflow.


  var gr = new GlideRecord('u_supplier_task');


  gr.addQuery('parent', current.sys_id);


  gr.addEncodedQuery('state!=3^state!=4'); //NOT Closed Complete or Closed Incomplete


  gr.addQuery();


  gr.query();


  while (gr.next()){


  gr.close_notes='Arbetsorder har automatiskt stängts när SCM avbrutit flödet för Ny Leverantör';


  gr.state = 4; //Closed Incomplete


  gr.setWorkflow(false);


  gr.update();


  }



  //Cancel workflow


  new Workflow().cancel(current);



  current.active=false;


  current.state=-7;   //Review


  current.close_notes='Flödet för Ny Leverantör har avbrutits av SCM, alla icke utförda arbetsorder har automatiskt stängts';


  current.update();


  action.setRedirectURL(current);


  gs.addInfoMessage('The New Supplier Process has been canceled.');


}


View solution in original post

17 REPLIES 17

I still get the same behaviour, the remaining tasks are closed but the close note is blank.



Could it be an issue that the close notes field is read only unless state is Closed Complete (value 3)?


But it still doesn't make sense since it's possible to write to the field if I cancel the workflow at row 32 instead.


Do you have access control on close_notes field? Do you have any business that closes the main request once the tasks are closed?


We've got a UI Policy that makes the close notes field read only unless state is "closed complete".



The last step in the workflow is to set the ticket into state review, this doesn't happen with the script above. The state will be Closed Incomplete if I change line 17 to:


current.state=4;   //CLosed Incomplete




It just seems that part of the workflow keep running but it doesn't complete the whole workflow before the


new Workflow().cancel(current);


is triggered (when it's inserted at row 32)


I think its much easier to set those types of values on the form/client site before the submit


so before line 04, add something like


var myclose_notes +"my text";


g_form.setValue("close_notes", myclose_notess);       //Set the field value before we submit


If I do it like this it works fine


function cancel(){


  if(confirm('Are you sure you want to cancel the New Supplier process?')){


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


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


  return true;   //Abort submission


  }


  return false;


}



// Server side code


if(typeof window == 'undefined')


  cancelNewSupplier();



function cancelNewSupplier(){


  //Set clsoe notes to all tasks.


  var gr = new GlideRecord('u_supplier_task');


  gr.addQuery('parent', current.sys_id);


  gr.addEncodedQuery('state!=3^state!=4'); //NOT Closed Complete or Closed Incomplete


  gr.addQuery();


  gr.query();


  while (gr.next()){


  gr.close_notes='Arbetsorder har automatiskt stängts när SCM avbrutit flödet för Ny Leverantör';


  gr.update();


  }



  current.active=false;


  current.state=-7;   //Review


  current.close_notes='Flödet för Ny Leverantör har avbrutits av SCM, alla icke utförda arbetsorder har automatiskt stängts';


  new Workflow().cancel(current);



  //Close all active supplier tasks and.


  var gr = new GlideRecord('u_supplier_task');


  gr.addQuery('parent', current.sys_id);


  gr.addEncodedQuery('state!=3^state!=4'); //NOT Closed Complete or Closed Incomplete


  gr.addQuery();


  gr.query();


  while (gr.next()){


  gr.state = 4; //Closed Incomplete


  gr.update();


  }



  current.update();


  action.setRedirectURL(current);


  gs.addInfoMessage('The New Supplier Process has been canceled.');


}



First I only set the close notes to the tasks.


After that the workflow will be canceled


Last thing to do is to set the state to closed incomplete for all tasks when workflow is canceled.



The solution is working but I assume there should be a much better way to do it?


I tried your suggestion but never got it to work. The workflow is triggered on the ticket, and the close notes should go into the tasks related to the ticket.