Categories
Best Practices Bizagi Tips and Tricks

Cancel case – the proper way

Have you ever wondered what is the proper way to cancel a case in Bizagi? When I first started working as a technical BPM consultant, I learned to cancel the case using the following workflow pattern:

cancel

At the beginning of the process, you add a parallel gateway. One path takes the normal flow, while the other one goes into this cancel logic: an Intermediate event followed by a Terminate event.

After some thoughts, I’ve come up with a better version:

As you’ve noticed, the change consists of a new Event-based gateway, a Conditional event and an extra Terminate event. It allows you to disable the Cancel option. In some situations, you will need to disable the cancel option, and with the above logic, you will be able to do this.

Now having all of these in place still doesn’t follow the best practices. And you may probably ask yourself why. The answer is quite simple and is related to reports.

Bad reports

When a case is cancelled following the above logic, in the database the case is marked as completed. Yes, with the status ‘Completed‘ and that’s not correct.

SELECT 
	A1.radNumber as 'Case Number',
	A2.csName AS 'Case Status' 
FROM 
	WFCASE A1 
LEFT OUTER JOIN
	CASESTATE A2
ON
	A1.idCaseState = A2.idCaseState

So when your business users run a report searching through all cancelled cases they will find only the ones that were cancelled from the Admin panel: Admin > Process Management > Cases.

Here is an example. Case no. 1 was completed following the normal workflow, meanwhile, case no. 51 was cancelled. In the database, these two cases have the same status.

The solution

The trick is to cancel the case before the token gets to the Terminate event. And for this, we will use a CHelper function called AbortProcess.

/*
** The abort options parameter receives the following options:
** 1- Abort the process and its children.
** 2- Abort the process, its children, parent, and siblings.
** 3- Abort the process's parent process, its siblings and children excluding the process received as parameter.
*/
var iCaseId = Me.Case.Id;
var iAbortOptions = 1;
var sAbortReason = 'The case was aborted via the SOA layer';

CHelper.abortProcess(Me, iCaseId, iAbortOptions, sAbortReason);

Save the above code into an expression and call it on the exit of the Intermediate event.

Now, when you cancel the case the status will be Canceled in the UI and Aborted in the database.

If you would like to find out more about Bizagi please send us an email.