How can I differentiate between Business Rule and Script Include?

gagandeeps123
Giga Contributor

Define Business Rules and Script Includes in ServiceNow. Highlight their key purposes within the platform.

3 REPLIES 3

Mike_R
Kilo Patron
Kilo Patron

https://developer.servicenow.com/dev.do#!/learn/courses/utah/app_store_learnv2_scripting_utah_script...

 

Business Rules

Business Rules are server-side logic that execute when database records are queried, updated, inserted, or deleted. Business Rules respond to database interactions regardless of access method: for example, users interacting with records through forms or lists, web services, or data imports (configurable). Business Rules do not monitor forms or form fields but do execute their logic when forms interact with the database such as when a record is saved, updated, or submitted.

Business Rule Configuration

To see all Business Rule configuration options, select the Advanced option.

 

 

 

  • Name: Name of the Business Rule.
  • Table: Specifies the database table containing the records this logic will run against.
  • Application: Name of the application the Business Rule is part of.
  • Active: Enables/disables
  • Advanced: Select to display all Business Rule configuration options.

When to run Section

  • When: Select when the Business Rule logic executes relative to the database access.
  • Order: Order of execution for Business Rules for the same table. Execute in ascending order. By convention, but not required, use Order values in round values of one hundred: 100, 200, 300, etc.
  • Insert: Select to execute the Business Rule logic when new records are inserted into the database.
  • Update: Select to execute the Business Rule logic when records are modified.
  • Delete: Select to execute the Business Rule logic when records are deleted.
  • Query: Select to execute the Business Rule logic when the database table is queried.
  • Filter Conditions: Add a condition to the configuration such as State is 14. The Filter Conditions must return true for the Business Rule logic to execute.
  • Role conditions: Select the roles that users who are modifying records in the table must have for this business rule to run.

 

https://developer.servicenow.com/dev.do#!/learn/learning-plans/utah/new_to_servicenow/app_store_lear...

 

Script Includes

Script Includes are reusable server-side script logic that define a function or class. Script Includes execute their script logic only when explicitly called by other scripts. There are different types of Script Includes:

  • On demand/classless
  • Extend an existing class
  • Define a new class

Configuring a Script Include

Script Includes do not have many configuration options because they are called rather than triggered.

 

 

 

 

  • Name: Name of Script Include.
  • API Name: The internal name of the Script Include. Used to call the Script Include from out-of-scope applications.
  • Client callable: Select this option if client-side scripts can call the Script Include using GlideAjax.
  • Application: The application the Script Include is part of.
  • Caller Access: When the Scoped Application Restricted Caller Access(com.glide.scope.access.restricted_caller) plugin is installed, allow scoped applications to restrict access to public tables and script includes.
    • --None--: No restrictions on access.
    • Caller Restriction: Do not allow access unless an admin approves access by the scope.
    • Caller Tracking: Allow access but track which scopes access the Script Include.
  • Accessible from: Choose This application scope only or All application scopes. Specifies the scope(s) that can use the Script Include.
  • Active: Select if the Script Include is executable. If this option is not selected the Script Include will not run even if called from another script.
  • Description: (optional but highly recommended) Documentation explaining the purpose and function of the Script Include.
  • Protection policy: If set to Read-only, instances on which the application is installed from the ServiceNow Store can read but not edit the Script Include. If set to Protected, the Script Include is encrypted on instances on which the application is installed from the ServiceNow Store. Protection policies are never applied to the instance on which an application is developed.

 

 

 

Danish Bhairag2
Tera Sage
Tera Sage

Hi @gagandeeps123 ,

 

1. **Business Rules:**

   - **Definition:** Business Rules in ServiceNow are server-side scripts that execute when records are displayed, inserted, updated, or deleted. They are used to automate processes and enforce business logic without modifying the form layout.

   - **Key Purposes:**

     - **Automation:** Business Rules automate processes by defining conditions and actions that should be performed when those conditions are met. For example, setting default field values, enforcing data validation, or triggering additional actions based on certain conditions.

     - **Data Integrity:** They ensure data integrity by validating and transforming data before it is stored or displayed, maintaining consistency and accuracy in the ServiceNow instance.

     - **Streamlining Processes:** Business Rules help in streamlining workflows and processes by automating repetitive tasks, reducing manual effort, and improving efficiency.

 

2. **Script Includes:**

   - **Definition:** Script Includes in ServiceNow are reusable JavaScript classes that contain functions and properties. They encapsulate server-side logic and can be referenced and reused in various scripts, including Business Rules, Client Scripts, and UI Policies.

   - **Key Purposes:**

     - **Code Reusability:** Script Includes promote code reusability by encapsulating common logic, making it easy to maintain and update shared functionalities across different parts of the ServiceNow platform.

     - **Modularization:** They facilitate modular programming by organizing complex logic into separate Script Includes, making the codebase more manageable and understandable.

     - **Enhanced Performance:** Script Includes can be cached, leading to improved performance since the logic is loaded once and can be reused across multiple scripts, reducing the overall server load.

 

Let's delve into examples for both Business Rules and Script Includes in the context of a fictional IT Service Management application within ServiceNow.

 

### Business Rules Example:

 

**Scenario:** Whenever a high-priority incident is created, automatically assign it to the support group manager for immediate attention.

 

**Business Rule:**

```javascript

// Business Rule Name: Assign High Priority Incidents

// Table: Incident

// When to Run: Before Insert

 

(function executeRule(current, previous /*, /*other parameters*/) {

    if (current.priority == 1 /* High Priority */) {

        current.assignment_group = getSupportGroupManager(); // Custom function to retrieve support group manager

    }

})(current, previous);

```

 

**Explanation:**

- This Business Rule triggers before a new incident record is inserted.

- It checks if the incident's priority is set to high (priority 1) and automatically assigns it to the support group manager using a custom function.

 

### Script Includes Example:

 

**Scenario:** Validate if a given user has the necessary permissions to approve a change request.

 

**Script Include:**

```javascript

// Script Include Name: ChangeRequestUtils

var ChangeRequestUtils = Class.create();

 

ChangeRequestUtils.prototype = {

    initialize: function() {},

 

    hasApprovalPermission: function(user) {

        // Check if the user has necessary approval permissions

        // Logic to verify permissions...

        return true; // Return true if the user has permission; false otherwise

    },

 

    // Other utility functions can be added here

    type: 'ChangeRequestUtils'

};

```

 

**Usage in Business Rule:**

```javascript

// Business Rule Name: Validate Change Request Approval

// Table: Change Request

// When to Run: Before Update

 

(function executeRule(current, previous /*, /*other parameters*/) {

    var changeRequestUtils = new ChangeRequestUtils();

    if (!changeRequestUtils.hasApprovalPermission(current.approver)) {

        current.approval = 'rejected';

        current.comments = 'Insufficient permissions for approval.';

    }

})(current, previous);

```

 

**Explanation:**

- The `ChangeRequestUtils` Script Include contains a method `hasApprovalPermission(user)` that checks if a user has the necessary permissions to approve a change request.

- In the Business Rule, it creates an instance of `ChangeRequestUtils` and calls the `hasApprovalPermission()` method to validate if the approver has the required permissions. If not, the change request is automatically rejected with a comment explaining the reason.

 

These examples illustrate the use of Business Rules for automating actions based on record conditions and Script Includes for encapsulating reusable logic, promoting modularity, and enhancing code readability and maintainability.

 

 

In summary, Business Rules are primarily focused on automating and enforcing business logic within records, ensuring data integrity and efficient processes. Script Includes, on the other hand, promote code reusability and modularization, enhancing the maintainability and performance of scripts within the ServiceNow instance.

 

Please mark my answer helpful & accepted if it helps answer your question.

 

Thanks,

Danish

Nivedita Patil
Mega Sage
Mega Sage

Hi @gagandeeps123 ,

 

Business Rule:

Business Rules are server-side logic that execute when database records are queried, updated, inserted, or deleted. Business Rules respond to database interactions regardless of access method: for example, users interacting with records through forms or lists, web services, or data imports (configurable). Business Rules do not monitor forms or form fields but do execute their logic when forms interact with the database such as when a record is saved, updated, or submitted.

Types of business rule:

  • before
  • after
  • async
  • display

 

Script Include:

Script Includes are reusable server-side script logic that define a function or class. Script Includes execute their script logic only when explicitly called by other scripts.

There are different types of Script Includes:

  • On demand/classless
  • Extend an existing class
  • Define a new class

We can call script include from client side using GlideAjax.

 

Mark my answer correct and helpful if helps  you.

 

Thanks,

Nivedita Patil.