If the business rule you created is not working as expected, here are some key points to double-check and a more detailed step-by-step guide to troubleshooting:
Step-by-Step Guide to Creating the Business Rule
-
Navigate to Business Rules:
- Go to
System Definition > Business Rules and click "New" to create a new business rule.
-
Basic Information Configuration:
- Name: Provide a unique and meaningful name for your business rule.
- Table: Select the table where you want this rule to apply, e.g.,
Incident.
- Active: Ensure the rule is active by checking the
Active checkbox.
- Advanced: Check the
Advanced checkbox to enable scripting sections.
-
When to Run:
- When: Select "before".
- Insert: Check this to apply the rule when a new record is inserted.
- Update: Check this to apply the rule when a record is updated.
-
Conditions:
- Define specific conditions if you want the rule to apply only in certain cases, or leave it blank to apply universally.
-
Script Configuration:
- Go to the
Advanced tab and add the following script:
Example Script
(function executeRule(current, previous /*null when async*/) {
// Replace 'short_description' with your field's name
var mandatoryField = 'short_description';
// Check if the mandatory field is empty
if (gs.nil(current.getValue(mandatoryField))) {
// Add an error message
gs.addErrorMessage('The ' + mandatoryField + ' field is mandatory.');
// Abort the action
current.setAbortAction(true);
}
})(current, previous);
Validations and Troubleshooting Steps
- Field Name: Ensure the field name is correct. For example, make sure
'short_description' matches exactly the field you are targeting in the table schema.
- Record Actions: Verify that the rule is set to trigger before
insert and/or update based on your requirement.
- Active Field: Confirm that the
Active checkbox is checked.
- Message Display: Check whether the error message is visible in the UI. There may be UI configuration or form layout issues that prevent the message from displaying properly.
- Debugging: Add debugging logs for more insight:
(function executeRule(current, previous /*null when async*/) {
var mandatoryField = 'short_description';
gs.log('Business Rule Triggered for Record: ' + current.sys_id);
// Check if the mandatory field is empty
if (gs.nil(current.getValue(mandatoryField))) {
// Add an error message
gs.addErrorMessage('The ' + mandatoryField + ' field is mandatory.');
gs.log('Mandatory Field Check Failed: ' + mandatoryField);
// Abort the action
current.setAbortAction(true);
}
else {
gs.log('Mandatory Field Check Passed: ' + mandatoryField);
}
})(current, previous);
- Log Analysis: Check the
System Logs > System Log > All for the logs you added (e.g., Business Rule Triggered and Mandatory Field Check Failed).
-
User Role and Access: Ensure the user testing the script has the appropriate permissions to see error messages and that no UI policies or client scripts are conflicting with this server-side logic.
-
Script Syntactic Errors: Ensure there are no syntax errors in the script that might prevent it from executing.
By carefully following these steps and checking each part of your script and configuration, you can troubleshoot and resolve the issues causing the business rule to not function correctly. If the problem persists, consider consulting ServiceNow documentation or reaching out to a ServiceNow expert for more tailored assistance.