Lesson 5
5. Debugging business rule
Business Rules are server-side scripts in ServiceNow that run when a record is inserted, updated, deleted, or queried. Debugging a Business Rule helps identify logic errors, script failures, or unexpected behavior.
-
Enable Debugging:
Navigate to System Diagnostics > Session Debug > Debug Business Rule (Details).
-
Trigger the Business Rule:
Perform the action that causes the business rule to execute (e.g., creating, updating, or deleting a record).
-
Analyze Output:
The Session Log will display detailed information about the business rule’s execution, including:
- Which business rules ran and in what order.
- Whether they passed or failed.
- Field values that were changed by the business rule (old and new values).
- Detailed execution flow.
- Data mutation tracking.
- Which business rules ran and in what order.
-
Important Note:
Detailed debugging shows field values changed by the BR, using the syntax
<field name>: <old value> => <new value>.
gs.log Statements:- Insert
gs.logstatements: Addgs.log()orgs.info()statements at various points within your business rule script to output variable values or messages to the system log. - Check the System Log: After the business rule executes, check the system logs (System Logs > System Log > All) to view the output of your
gs.logstatements. - Example:
gs.log("The value of variable 'x' is: " + x);
- Access the Tester: The Business Rule Tester allows you to test business rules in a controlled environment, separate from the production instance.
- Set up the Test: Configure the tester with the appropriate table, record, and action.
- Analyze Results: The tester will show the results of the business rule execution, including any changes made to the record.
-
Filter Logs:
When using the system log, filter by table, activity type, and time frame to narrow down the search for relevant log entries.
-
Table Watcher:
Consider using the Table Watcher plugin for real-time monitoring of table activity and identifying which business rules are triggered.
-
Security Debugging:
If security rules are involved, enable the security debugger to understand how access controls affect the business rule execution.
-
Consider Query Business Rules:
If records are missing, it might be due to a query business rule. Check the Session Debugger output to see if any “before query” business rules are filtering out records.
-
Check for Errors:If you’re not seeing expected output, ensure that your script is not crashing and that you’re using the correct methods for logging and displaying messages (e.g.,
gs.addInfoMessagefor user-facing messages andgs.logfor system logs).
