Categories
Bizagi Tips and Tricks

How to Trace like a Pro

During the development phase of the project, and sometimes in the Production environment, you find yourself in a situation when errors need to be traced. Or as I call it, you need to debug the application.

In Bizagi, the easiest way to debug is to use the Trace functionality. Now, this is a simple CHelper method where you have to pass 2 arguments:

CHelper.trace(string sFileName, string sText)

…where the sFileName is the name of the .txt file where the traces will be stored and the sText is the information to trace. It can be an attribute selected by a navigational XPath expression, a function, or a variable.

Trace like a Pro

How should we call this method? When show we call it? What are the best practices? These are a few questions probably everyone has when they start coding in Bizagi.

Having the method used inside a Global library rule will allow us to set a standard on how to call the Trace, on how the file output should look like and so on.

After some years of experience, I can guarantee that consistency is one thing to look after. Imagine, having no consistency in file naming or in the output trace. It will be hard for anyone looking at them to really understand what happens behind the hood.

If it’s hard to debug, then there’s a small change to find and fix that bug and we don’t want that. Why? Because we are Pros 😉

The solution

Go to Bizagi Studio > Expert > Business Rules > Library Rules

Bizagi Library Rules

First, let’s create a new Rule Family property and name it Global. Here will create and store all the global library rules we intend to use in our application.

Global Rule family property

In this example, we will create the Trace library rule. Select New Library rule and fill in the following details.

1) Display Name, Name, Description. It’s always a good practice to state the initials of the person who created the rule, plus a summary of what the rule is supposed to do. When you will have an issue, you will know without opening the rule that it was created by X and it’s supposed to do Y, meaning that functionality is broken.

Global Trace library rule

2) Set the Rule Family to Global. It means the new rule will be placed under the Global category.

3) Set the output of the rule to Boolean

4) Set the input parameters: sRuleName, sMessage, and iLevel

Create a new global library rule: Trace

Use the following code inside the rule.

var sFileName, sLogText;
sFileName = "Trace";
if ((Me.Case.CaseNumber == null) || (Me.Case.CaseNumber == "")) {
	sFileName = "NoCaseNumber_" + Me.Case.Id.ToString();
} else {
	sFileName = Me.Case.CaseNumber;
}

sLogText = "Rule: " + sRuleName + "; ";
if (iLevel == 1) sLogText += " Information: ";
if (iLevel == 2) sLogText += " Warning: ";
if (iLevel == 3) sLogText += " Error: ";
sLogText += "Message: " + sMessage;
CHelper.trace(sFileName, sLogText);

return true;

How to call the library rule

On your process, go on any user task and add for this example, a new rule on Save.

Inside the rule, add the following code.

/*
Auth:	Valeriu B
Desc:	09 Oct 2021 - test the trace functionality
*/
var sRuleName = "PLT_OnSave";
Global.Trace(Me,sRuleName,"START --------------------------------------------------------------",1);

var sFullName = Me.Case.WorkingCredential.FullName;
Global.Trace(Me,sRuleName,"Fullname for the current user: "+ sFullName,1);

Global.Trace(Me,sRuleName,"END ----------------------------------------------------------------",1);

As a best practice, add the person who wrote the code and the date when it was written, plus a summary of what the code is supposed to do. If later, you make amendments, don’t forget to add another line after the Desc, with the name, date and description of the change.

Cool, now let’s run the code and see the trace.

C:\Bizagi\Projects\[Name of the project]\Trace

As you can see, by just looking at the trace we know that case 101 has run and the latest call was made for the PLT_OnSave rule, where we traced the Start of the rule, the current user’s full name and the End of the rule.

When you debug, go to the last line, identify the last rule name and see if the END message is displayed. If not then you know for sure the issue is within this rule.

Of course, 101 is a bad naming convention. So, what I am going to do next is show you how to set a different case name depending on the process.

Traces in Bizagi

Enable traces in Bizagi

If you don’t see the traces, don’t panic. Probably, they are not enabled. Go to Bizagi Studio > Configuration > Tracing and select Rules and expressions, then select All. It should look like this.

Enable traces in Bizagi for rules and expressions