Before Business Rules in ServiceNow: Scenarios, Scripts, and Best Practices

Before Business Rules
 

What are they?

Before a record is added, updated, or removed from the database in ServiceNow, business rules must first be executed. Before a transaction is committed, they are necessary for validating data, enforcing data integrity, and taking appropriate action based on predetermined criteria.

Advanced

Real-time Use Cases and Scenarios
 

Scenario 1. Determining Incident Priority

Scenario: You want to automatically prioritize incidents in a ServiceNow instance for IT Service Management (ITSM) based on their impact and urgency.

Use Case

  1. Goal: Ensure that incidents with a high impact and a need for an urgent resolution are appropriately prioritized.
  2. Implementation: Before Business Rule: Make an entry in the incident table (incident) for the before business rule.
  3. Conditions: When the impact or urgency fields are changed, the rule is triggered.
  4. Action: Use a script to determine the priority (priority field) based on impact and urgency.
  5. Validation: Verify that the determined priority satisfies the specified standards (e.g., Critical, High, Medium, Low).
    (function executeBefore(current, previous) {
        var impact = current.impact;
        var urgency = current.urgency;
        var priority = 'Medium'; // Default
        if (impact == 1 && urgency == 1) {
            priority = 'Critical';
        } else if (impact == 1 || urgency == 1) {
            priority = 'High';
        } else if (impact == 2 && urgency == 2) {
            priority = 'Medium';
        } else {
            priority = 'Low';
        }
        current.priority = priority;
    })(current, previous);
    

Scenario 2. Stopping Unauthorized Modifications

Situation: Depending on their role, you would like to prevent users from changing specific fields on a record.

Use Case

  1. Goal: Preserve data integrity by limiting illegal modifications to vital fields.
  2. Execution: Before Business Rule: Establish a before-business rule on the table (such as an incident) where it is necessary to restrict unauthorized updates.
  3. Conditions: When particular fields (like assigned_to or state) are changed, the rule is triggered.
  4. Step: Verify the user's role; if they don't have the necessary role (like ITIL), deny the update.
    (function executeBefore(current, previous) {
        var user = gs.getUser();
        var allowedRoles = ['ITIL'];
        if (!gs.hasRole(user, allowedRoles)) {
            // Prevent update
            gs.addErrorMessage('Unauthorized: Only users with ITIL role can update this field.');
            current.setAbortAction(true);
        }
    })(current, previous);
    

Scenario 3. Calculating Dynamic Fields

Situation: Given inputs from other fields, you must compute a dynamic field value.

Use Case

The purpose of automating a derived field's calculation is to guarantee accuracy while streamlining data entry.

  1. Execution: Before Business Rule: On the table that contains the derived field, create a before business rule.
  2. Conditions: When input fields (like price or quantity) are changed, the rule is triggered.
  3. Action: Using the inputs as a guide, determine the value for the derived field (such as total_cost).
    (function executeBefore(current, previous) {
        var price = current.price;
        var quantity = current.quantity;
        var totalCost = price * quantity;
        current.total_cost = totalCost;
    })(current, previous);
    

Scenario 4. Error handling and validation

Scenario: Before saving records, validate data inputs and correct errors.

Use Case

  1. Goal: Maintain data consistency and give insightful error messages for inaccurate inputs.
  2. Execution: Before Business Rule Make a before business rule (such as change_request) on the table that needs to be validated.
  3. Conditions: To validate particular fields (like planned_start_date and planned_end_date), the rule must be triggered when the record is saved.
  4. Step: Verify that the scheduled start and end dates coincide, and correct any inconsistencies that arise.
    (function executeBefore(current, previous) {
        var startDate = current.planned_start_date;
        var endDate = current.planned_end_date;
        if (startDate && endDate && startDate >= endDate) {
            gs.addErrorMessage('Planned start date must be before the planned end date.');
            current.setAbortAction(true);
        }
    })(current, previous);
    

The Best methods for Earlier business regulations

  • Maintain Script Efficiency: To reduce execution time and enhance system performance, write brief scripts. (the shorter the better)
  • Use Transactions Wisely: To prevent unforeseen side effects, exercise caution when performing transactional operations on current and previous objects.
  • Use Logging: To track rule executions for auditing and debugging purposes, including logging (gs.log).
  • Limit Abort Actions: To prevent unnecessarily interfering with user workflows, use setAbortAction(true) only infrequently.
  • Test Extensively: To verify rule behavior in various scenarios, carry out extensive testing in non-production environments.

Priority business rules in ServiceNow are essential for upholding data integrity, automating procedures, and preventing unauthorized actions—all of which are necessary for enforcing business logic and improving user experience. Organizations may efficiently streamline operations and enhance overall service delivery by putting these regulations into place with careful planning and consideration of particular use cases.

Adminssz and developers can meet a variety of business requirements in their ServiceNow environment by utilizing the examples and scripts that are provided to them. This allows them to fully utilize the power of before-business rules. Hope this was useful, thanks, and bye until next time.


Similar Articles