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

Did you try what Robert suggested like below ?



function cancel(){  


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


{  


g_form.setValue("close_notes", 'any text here');


gsftSubmit(null, g_form.getFormElement(), 'cancel_new_supplier');


}  


else


{


  return false;


}  


}


Hi, if I do it like that it doesn't do anything at all.


Since the UI Action is on the u_supplier_ticket table and I want the close notes on the u_supplier_task table (which is extended from u_supplier_ticket) I would assume this won't work anyway.


Was this ever mentioned before that you want to update notes on task ? I might have missed it, if it was..



  1. while (gr.next()){  
  2.   gr.state = 4; //Closed Incomplete  
  3. gr.close_notes='Some Text';
  4.   gr.update();  
  5.   }  

This is from the opening posts, maybe I wasn't clear enough



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).



Problem is when I do it like that (which is the most logical way to do it) it will keep on creating all the remaining tasks in the workflow and close them right away as well since the workflow isn't already canceled.



When I try to cancel the workflow before doing anything to the tasks it won't write the close notes but it will change the state. Se my post from Sep 3, 2015 11:12 AM


Okay .. I guess some business rule is misbehaving here .. Follow the below approach and let me know how it goes.



Query and Close all the task first and use the below lines to add the closure notes and stop any business rules from running.



  • gr.close_notes='Some Text';
  • gr.setWorkflow(false);
  • gr.update();



Next, cancel the workflow


new Workflow().cancel(current);  




And now update the details on the main request




  1. current.active=false;  
  2. current.state=-7;   //Review  
  3. current.close_notes='Flödet för Ny Leverantör har avbrutits av SCM, alla icke utförda arbetsorder har automatiskt stängts';  
  4. current.update();  
  5. action.setRedirectURL(current);  
  6. gs.addInfoMessage('The New Supplier Process has been canceled.');



This should work