K2 Blackpearl Infopath/Sharepoint Workflow- Cancel Workflow Anytime by Requester

  • 7 September 2011
  • 4 replies
  • 0 views

Badge +3

Here is a common requirement which many asks, to be Able to cancel the workflow by requester anytime during the approval process. 


example, I have a two level approval whre the form gets assigned to Manager 1 and then to Manager 2.  The Requester should be able to cancel the workflow anytime . 


The workflow diagram is attached below.


Problem is, the workflow will not end if you go through the normal approvals and reaches end stage (since there is one active activity "cancel workflow" which is assigned to originator ).


I saw few articles but wanted to know the best approach to solve this case.


Please share your thoughts.


{If you go through cancel activity line, i added a dummy escalation to trigger and complete the activity as workaround to complete the workflow when user cancels the workflow}


 


15009i54A01477EB398AB7.png

4 replies

Badge +10

Hi,


You are right, lots of people request this functionality.


The simplest way to do this would be to have an extra line from your Start activity to an activity which contains an Asynchronous Server Event. (K2.Synchronous = False). I would use something like a SmartObject event which writes the Serial Number for the event to a SmartObject (because you will need the serial number later to finish this event) and then make this event Asynchronous in the code-behind. The Serial Number consists o the ProcessInstanceID an underscore '_' and the ActivityDestinationInstanceID. All of this is available through the Workflow Context on the K2 Object/Context Browser. When the process starts the serial number is saved to the SmartObject and this branch of the process will wait until it is finished by another system (your front-end).


Below the Event that waits, create another Server Event and in the code behind perform a GotoActivity and let the process go to an Activity which will complete the process. A GotoActivity will expire all active Activities in the process and should clean up all the other Activities on the process.


From your front-end, if the Cancel Workflow is selected, search your SmartObject for the particular process ID which should be cancelled and get the Serial Number which was saved there. Use the SourceCode.Workflow.Client API and perform a OpenServerItem({SerialNumber}) and then a ServerItem.Finish() so that the waiting branch can continue.


To perhaps keep your SmartObject lean, clean up all the records for the process in the Activity which will complete the process. This will ensure that the SmartObject only contains records for processes that are currently active.


I've used this at a K2 Customer before and it works rather well.


I hope it all made sense.


Regards,

Badge +3

Hi RaghavendraK,


We had a similar required in one of our project and here is how we resolve this issue.


You can use a Default Server Event (code) with the following code for the activity you want to you want remove. This will remove the activity from the user worklist. You just need to add this code on the cancel path for all the approval activity and on the all approve path for the cancel activity.


            try
            {
                K2.ExpireActivity("ActivityName");
            }
            catch
            { }


The only down side of this solution is that the user is not notified that an activity was removed from his worklist (usually not an issue). The view flow will display the expired activity in red.


Please let us know if this works for you.


Cheers

Badge +10

This is where the K2.GotoActivity() is useful.


Using this method it will expire all activities and you do not need to edit all your other Activities to add the K2.ExpireActivity() method.


Only problem with the GotoActivity is that the Viewflow will show that the "Complete" activity was started without any lines being green into it.

Badge +4

Hi,
Why not use goto activity method? Goto activity each for your normal flow and cancel flow expires the activities that branched out in parallel. Whichever branched flow completes first that should expire the active activity.

K2.GotoActivity("ActivityName", true);
First parameter takes the activity name where it’s supposed to go and second parameter if set true will expires all active activities.


Some more information here.


 

Reply