Business Rules
A business rule is an action run on the server-side when a record is inserted, updated or deleted. Business rules are executed in a database transaction, regardless of the interface used to initiate the transaction (a user submitting a form, the execution of a server-side script, a REST request, the execution of a Workflow Activity block). The rule action can be executed both on the current record and on other records.
Business rules can use scripts to perform actions with records in the database. There are other options of the script actions available, such as client-side scripts and UI actions.
Business rules are used for:
- changing values in the fields of a record that caused the rule to run when certain conditions are met.
- execution of a server-side script of the rule.
- displaying a message on the client side.
- creating system events to send e-mail notifications or run other server-side scripts.
- interrupting database transactions.
To view all business rules navigate to System Settings → Business Rules.
To view the business rules related to a specific table, complete the steps below:
- Navigate to the list or form view of the table.
- Select Configure → Business rules from the burger menu.
Business rule types
Depending on the business task you are solving, use one of the following types of business rules:
- before
- after
- async after
Before rules run conditions:
- After record action start (insert, update, or delete).
- Before the system performs server updates (before the database transaction is completed).
After rules run condition:
- After the system performs server updates (after the database transaction is completed).
Async after rules run condition:
- After all business rules of other types.
The async after business rules run after the before and after business rules, so the user first receives the form with all the changes, and then the async after rules run. Thus, using the async after business rules, you can optimize system response time – use the async after rules for resource-intensive scripts that take a lot of time.
The following scheme illustrates when the before, after, and async after rules run. All rules are executed after the start of an action on a record (insert, update, or delete).
The process starts with a form action: if it is an update action, the data source is the database, so the record has the previous block. The previous block represents the form state before the action (form submission – insert, update or delete), and the current blocks represent the form state after the action is completed. Form submission can be performed by a user, a script or a REST API request.
Create a business rule
Role required: admin.
To create a new business rule, perform the steps below:
- Navigate to System Settings → Business Rules.
- Click New and fill in the fields.
- Click Save or Save and exit to apply the changes.
Business rules form fields
- General
- When to Run
- Action
- Advanced
Field | Mandatory | Description |
---|---|---|
Name | Y | Specify a name for the business rule. |
Table | Y | Select the table for which the business rule is executed. |
Description | N | Add a brief description of the action. |
Active | N | Select this checkbox to activate the business rule. |
Inheritance | N | Select this checkbox to apply this business rule to all child tables of the specified Table. |
Advanced | N | Select this checkbox to display the Script field and advanced settings such as the type of the business rule (the When field in the When to Run tab). |
Field | Mandatory | Description |
---|---|---|
When | N | Select the moment of the business rule execution:
|
Order | N | Specify the order of the business rule execution. If there are multiple business rules of the same type (before, after, async after), they are executed in the ascending order. This field appears when the Advanced checkbox is selected and the When field value is set to before or after. |
Priority | N | Specify the priority of the business rule. If there are multiple business rules with the same Order, they are executed in the descending order according to their Priority. This field appears when the Advanced checkbox is selected and the When field value is set to before or after. |
Action insert | N | Select this checkbox to execute the business rule when a new record is inserted. |
Action update | N | Select this checkbox to execute the business rule when a record is updated. |
Action delete | N | Select this checkbox to execute the business rule when a record is deleted. This field appears when the Advanced checkbox is selected. |
Filter conditions | N | Specify the conditions for the business rule execution. After specifying the Table, all fields of this table are available in the condition builder. Use the dynamic filter conditions like is (dynamic) or is not (dynamic) when setting up conditions to make your business rules more versatile. |
The Action tab is not available for the async after business rules.
Field | Mandatory | Description |
---|---|---|
Set value | N | Select a column and define the value you need. Available columns depend on the specified Table.
|
Abort action | N | Select this checkbox to interrupt the transaction (insert, update, or delete) when all conditions are met. It is a good practice to specify the reason for the interruption of the action. Select the Add message checkbox and add the text in the Message field. If no message is specified, the standard Saving error message is displayed when the action is aborted.If the abort conditions cannot be set using the Filter conditions field, use the script with the setAbortAction and ss.addErrorMessage methods. In this case, the Abort action checkbox should be cleared. When you use the setAbortAction() to abort a transaction, the script is not executed after calling this method. |
Add message | N | Select this checkbox to display the Message field if you need to show a message to a user when the business rule is performed. |
Message | N | Add a message you need to show to a user when the business rule is performed. The message is displayed as a toast message. To enable localization of the messages, use the values of the Source Message (source_message) records from the app category (determined by the value in the Category column). See Localization and Multi-language Support to learn more. This field appears when the Add message checkbox is selected. |
(function executeRule(current, previous /*null when async*/) {
current.update();
})(current, previous);
Field | Mandatory | Description |
---|---|---|
Script | N | Add a script that determines the actions to be executed when the business rule is launched. The previous and current variables are available:
|
When a record is inserted:
- previous is null.
- current is equal to the SimpleRecord object that will be inserted.
When a record is updated:
- previous is equal to the SimpleRecord object as it was before transaction started.
- current is equal to the SimpleRecord object at the moment when the rule execution starts.
When a record is deleted:
- previous is null.
- current is equal to the SimpleRecord object at the moment when the rule execution starts.
You can call the REM attributes in the business rules of before type. In this case, while the business rule is processing, you get the current attribute values, before the transaction occurs.
When the async after business rule is triggered, the action of the rule is sent to the queue. Because of this, there may be a slight delay between the triggering of the business rule and execution of the action. The current and previous objects in the async after business rules contain the state of the object at the time the business rule action is executed.
- Avoid using current.update() in the before or async after business rule scripts, as it may cause recursion. If you cannot avoid using current.update(), add a run condition for the rule that is not satisfied after the first execution of the rule. Otherwise, recursion is inevitable.
- Avoid using the setAbortAction method with the async after business rules.
- If a business rule script inserts or updates records in other tables, use the after business rule type. Otherwise, if a business rule fails during a transaction, the before rule inserts/updates a record in other tables which leads to data inconsistency.
The server-side business logic may not work for system tables if the creation or updating of records is initiated by the backend. For example, an Import Set record is created as part of loading an Import Source, or an Email is created as a result of a notification rule, and so on.
Example of tables for which server-side business logic may not work:
- Main Log (sys_log)
- Script Log (sys_log_script)
- Exception Log (sys_log_exception)
- Record Deletion Log (sys_record_deletion_log)
- Import Set (sys_import_set)
- Activity Feed Item (sys_activity_feed_item)
- Email (sys_email)
Abort business rule
It is possible to perform business rules that abort the subsequent actions and processes: business rules, notifications, approval rules, and other.
To make your business rule abort the follow-up processes, complete the steps below:
- Open a business rule record you need.
- In the When to Run tab, define the abort conditions.
- In the Action tab, select the Abort action checkbox.
- In the When to Run tab, check that the When field is set to before. The When field is displayed when the Advanced checkbox is selected.
- Click Save or Save and exit to apply the changes.
Override an out-of-the-box business rule
If necessary, some "out-of-the-box" business rules can be overridden.
- For applications other than Simple it is enough to copy an "out-of-the-box" rule using the Make a copy action and deactivate the original rule. You can define the required logic in the copy of the rule.
- For the Simple application, most rules are delivered with Protection policy = Protected, so you cannot deactivate them. However, the rules of the When = before type can be overridden by rules of a higher order with the same start conditions. For example, an "out-of-the-box" rule with the before type generates a value in the Subject field. The order of the rule is 999. To change the value generation logic, it is enough to make a copy of the rule using the Make a copy action, define a specific logic and set Order = 1000. If the value in the field should remain unchanged, use the rule triggered by an Action update event with the following script:
(function executeRule(current, previous = null /*not null only when action is update*/) {
if (previous) {
current.subject = previous.subject;
}
})(current, previous);