View Article

Use a Try Block in a Logical Rule and Test the Rule

By Xelence Documentation posted 12-14-2022 14:12

  

Introduction

This process uses a Try block in a Logical Rule. This handles runtime exceptions to prevent your app from crashing.

A try block contains a block of code that might raise an exception. If an exception occurs, the app will execute the nearest catch block. When the app is finished executing any catch blocks, it executes the finally block to close any resources opened in the try block.

You can add a try block to any new or existing rule. This example creates a new rule and designs it with the try block.

Prerequisites

The entity must exist.

Quick Steps
1 Click the Create New Item icon, then select the Object Type
2 Select the Rule Type
3 Enter the details, then click OK
4 Design the rule
5 Enter the Description and Expression for each component
6 Declare any variables
7 Save and compile the rule
8 Open the scenario, then open the test case
9 Enter the values and run the test
10 View the result diagram

Detailed Steps

Step 1. Click the Create New Item icon, select the Object Type, then select the Rule Type.

This opens a dropdown list from which you can select an object to begin its item wizard. The item wizard creates a file or files.

The Object Type identifies the file the wizard will create. In this case, it is Rule.



Step 2. Select the Rule Type.

The  Rule Type identify the file the wizard will create. This example creates a Logical Rule.



Step 3. Enter the details, then click OK.

Enter the following properties:

  • Entity: The rule is based on the entity. The fields available to the rule will be based on this entity's attributes.
  • Name (ID): This is the rule's programmatic ID. This property is required. The ID must be unique, and it cannot start with numbers.
  • Access Modifier: Specifies the level of accessibility of the rule.
  • Return Type: This is the data type of the result you expect the rule to return (string, int, etc.).
  • Is Collection: This is available when the logical rule returns a result. If checked, the rule can return a collection.
  • Static: If checked, the rule will be available to call from anywhere. Otherwise, you will have to call it from an instance of its object. A logical rule can directly use the entity attributes if it is not static.
  • Allow From Platform Api: If checked, the rule will be available from Platform API.
  • Trace: Making a rule traceable means you can view the path and result every time the code executes that rule.
  • Description: Optionally, you can enter a short description for your rule.
The Access Specifier options are:

Public: The rule is accessible from anywhere.
Private: The rule is accessible only from the current entity rules.
Protected: The rule is accessible only from this entity and any child entity.



Step 4. Design the rule.

Xelence creates a new logical rule as an empty field. You must build the flowchart for the rule by dragging and dropping components from the toolbox.

To add a Try block, drag the Try component from the toolbox.



Step 5. Enter the Description and Expression for each component.

For each component, enter the description of what it does in the rule, then enter the expression. 

When you drag a Try block to the rule, Xelence adds one Try block, Catch bock, and Finally block.

The Try block defines a block of code that might raise a runtime exception.

The Catch block defines a block of code that executes whenever an exception is raised.

The Finally block defines a block of code that executes regardless of whether executing the Try block raises an exception.



Step 6. Declare any variables.

If the rule uses variables, drag and drop the Variable Declaration component from the toolbox. You must add any undefined components as variables.

For each variable, you must enter the properties:

  • Data Type: Type of data the variable uses (string, int, bool, etc.).
  • Variable Name: String you are adding as a variable.
  • Default: If the variable is local, you can assign it a default value.
You can also declare the variables before you design the rule or configure the rule components.



Step 7. Save and compile the rule.

When you are finished defining the rule, click the Save icon. 


You must compile the rule so it can be called. Click the Compile Rule icon to compile the rule.


The rule is configured. You may now call it elsewhere in your project, based on its accessibility and static settings.



Step 8. Open the scenario, then open the test case.

If you want to test the rule to confirm it returns the value you expect, click the Navigate to Scenario icon.



Step 9. Enter the values and run the test.

Enter the input value for the test. Optionally, you can enter the value you expect the rule to return. Click the Run Test icon and select Current Rule to run the test step.

Xelence provides the following options for running the test:

Only Result: Displays only the result.
Current Rule: Displays the result and diagram for this rule.
Child Rule: Displays the execution of any child rule.



Step 10. View the result diagram.

If you want to trace the executed logic, click the View Test Result icon to open the result diagram.

In this example, since the entCustomer variable was kept default, assigning the Country Code is not successful. The rule raises an exception.

Run the test as many times as you want. This example updates the rule to initialize the variable so it will no longer raise the exception.


Now the rule executes successfully.




What are the improvements over S3 Version 6?

This is a new process.


Related Articles

Create and Test a Logical Rule with One Condition

Create and Test a Logical Rule with a Loop and Call to a Decision Table

Logical Rule Elements


This post is part of the Rules topic. Click here to open the Rules Overview.

#Xelence
#Rules

0 comments
83 views