Business Rules Made Easy: Everyday Use Cases

Community Alums
Not applicable

What Is a Business Rule?

A Business Rule is a server-side script in ServiceNow that executes when records in a table are displayed, inserted, updated, deleted, or table is queried.

Business Rules are stored in the sys_script table.

  • Use Cases of Business Rule:-
  1.  Prevent Duplicate Incident Creation in ServiceNow

Scenario: If we want to restrict users from creating a new incident if one already exists with the same Short Description and Caller ID.

  • Steps to Implement Business Rule
  1. Navigate to: All > Business Rules > New
  2. Configure the fields:
    • Name: Prevent Duplicate Incident
    • Table: Incident
    • Active: True
    • Advanced: True
  3. Set 'When to run':
    • Before: Insert
  4. Add the following script under the Advanced section:

 

var gr = new GlideRecord("incident");

gr.addQuery("short_description", current.short_description);

gr.addQuery("caller_id", current.caller_id);

gr.query();

if (gr.next()) {

  gs.addErrorMessage('You can’t create a new incident. A record already exists with the same caller and short description.');

  current.setAbortAction(true);

}

2. Auto-Populate Problem Task Fields from Parent Problem Record in ServiceNow

 

  Scenario: When creating a Problem Task directly from the related list of an existing Problem record, we often want to carry over key details—such as the Short Description, Description, Assigned To, and Configuration Item (CMDB CI). This business rule automates that process, saving time and ensuring consistency.

  • Steps to Implement the Business Rule
  1. Navigate to: All > Business Rules > New
  2. Fill in the required fields:
    • Name: Auto-fill Problem Task from Parent Problem
    • Table: Problem Task
    • Active: True
    • Advanced: True
  3. Set 'When to run':
    • Display
  4. Insert the following script in the Advanced section:

 

current.short_description = current.problem.short_description;

current.description = current.problem.description;

current.assigned_to = current.problem.assigned_to;

current.cmdb_ci = current.problem.cmdb_ci;

3. Automatically Close Associated Incident Tasks When Incident Is Resolved

Scenario: To ensure that related tasks don’t remain open unnecessarily, we can implement a Business Rule that automatically closes all associated Incident Tasks when the parent Incident is marked as Resolved.

  • Steps to Create the Business Rule
  1. Navigate to: All > Business Rules > New
  2. Configure the Rule Fields:
    • Name: Auto-Close Incident Tasks on Resolution
    • Table: Incident
    • Active: True
    • Advanced: True
  3. Set Execution Timing:
    • When to Run: After > Update
    • Condition: State changes to Resolved
  4. Add the Following Script to the Advanced Section:

var gr = new GlideRecord("incident_task");

gr.addQuery("universal_request", current.sys_id);

gr.query();

while (gr.next()) {

gr.setValue('state', 3); // 3 = Closed

gr.close_notes = 'Closed automatically via Business Rule.';

gr.update();

}
4. Automatically Remove Inactive Users from Groups in ServiceNow

Scenario: when a user becomes inactive in the User [sys_user] table, they are automatically removed from all groups they belong to. This Business Rule helps enforce that behaviour by deleting associated group memberships.

  • Steps to Implement the Business Rule
  1. Navigate to: All > Business Rules > New
  2. Configure the Rule Details:
    • Name: Remove Groups for Inactive User
    • Table: User (sys_user)
    • Active: True
    • Advanced: True
  3. Set Execution Timing:
    • When to Run: After > Update
    • Condition: active field changes to false
  4. Insert the Following Script in the Advanced Section:

var gr = new GlideRecord("sys_user_grmember");

gr.addQuery("user", current.sys_id);

gr.query();

while (gr.next()) {

gr.deleteRecord();

}

5. Auto-Creation of Problem Tasks When Priority Is Set to Critical

Scenario: automatically generates two Problem Tasks when the priority of a Problem is marked as 1 - Critical or changes to it. This ensures key activities like general analysis and root cause identification are initiated promptly.

  • Steps to Implement the Business Rule
  1. Navigate to: All > Business Rules > New
  2. Fill in the required fields:
    • Name: Auto-Create Tasks for Critical Problems
    • Table: Problem
    • Active: True
    • Advanced: True
  3. Set 'When to run':
    • After > Update
    • Condition: priority field changes to 1 - Critical
  4. Insert the following script in the Advanced section:

var type = ['General', 'Root Cause Analysis'];

for (var i = 0; i < 2; i++) {

var gr = new GlideRecord("problem_task");

gr.initialize();

gr.problem = current.sys_id;

gr.problem_task_type = type[i];

gr.short_description = 'Task Type: ' + type[i];

gr.insert();

}

0 REPLIES 0