Categories
Bizagi Tips and Tricks

How to cancel a case

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

Bizagi Modeler Cancel case workflow

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 an improved version:

Improved workflow version to cancel a case in Bizagi

As you’ve noticed, the change consists of a new Event-based gateway, an extra Intermediate event (display name: Disable, name: Event_6) and an End event. It allows you to disable the Cancel feature. In some situations, you will need to disable the cancel option, and with the above logic, you will be able to do this.

The Disable event can be triggered using the following expression rule in Bizagi:

var sRuleName = "DisableCancelEvent";
var sTargetEvent = "Event_6";

// --- trigger the event
try {
	CHelper.setEvent(Me, Me.Case.Id, sTargetEvent, null);
} catch(err) {
	CHelper.ThrowValidationError(err.message);
}

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.

Bizagi Work Portal Cancel case from Admin

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.

Bizagi case status in database

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.

Bizagi Actions

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

Bizagi Work Portal Case with a canceled status
Canceled case in the Work Portal
Bizagi case status in the database
Cancelled case in the database

Possible case statuses in Bizagi: Aborted, Completed, Initiated, NotInitiated, Running and Suspended.