ServiceNow Scripting and Development Basics – Business Rules, Client Scripts, and Script Includes


Learn ServiceNow scripting fundamentals using JavaScript. Understand GlideRecord, client-side vs server-side scripting, Business Rules, Client Scripts, and Script Includes with real hands-on examples

JavaScript for ServiceNow

JavaScript Fundamentals

ServiceNow uses JavaScript as its primary scripting language.

Core concepts used in ServiceNow:

  1. Variables and data types
  2. Functions
  3. Conditions and loops
  4. Objects and arrays

ServiceNow scripting follows standard JavaScript with platform-specific APIs.

GlideRecord

GlideRecord is the ServiceNow API used to interact with database tables.

Common GlideRecord operations:

  1. Query records
  2. Insert records
  3. Update records
  4. Delete records

Basic example:

  1. Query incidents where state is Open
  2. Update assignment group automatically

GlideRecord is server-side only.

Client-Side vs Server-Side Scripting

Client-Side Scripting

Runs in the user’s browser.

Examples:

  1. Client Scripts
  2. UI Policies

Used for:

  1. Field validation
  2. UI behavior
  3. Dynamic form changes

Server-Side Scripting

Runs on the ServiceNow server.

Examples:

  1. Business Rules
  2. Script Includes
  3. Background Scripts

Used for:

  1. Data processing
  2. Security enforcement
  3. Database updates

Business Rules

Overview

Business Rules execute server-side logic when records are inserted, updated, deleted, or queried.

Types of Business Rules

  1. Before: Runs before record is saved
  2. After: Runs after record is saved
  3. Async: Runs in background
  4. Display: Runs when form is loaded

Use Cases

  1. Auto-assign incidents
  2. Set default values
  3. Prevent invalid updates
  4. Sync data between tables

Best Practices

  1. Use Before rules when possible
  2. Avoid unnecessary queries
  3. Keep scripts simple and readable
  4. Use conditions to limit execution
  5. Avoid using Business Rules for UI logic

Hands-On: Auto-Update Incident Fields

  1. Navigate to System Definition → Business Rules
  2. Create a new Business Rule on Incident table
  3. Set type as Before Update
  4. Add condition (for example: Priority changes)
  5. Write script to update another field
  6. Save and test

Hands-On: Enforce Validations

  1. Create a Before Insert or Before Update Business Rule
  2. Add logic to block record save
  3. Use error messages to inform user
  4. Test validation behavior
  5. Client Scripts

Overview

Client Scripts run in the browser and control form behavior.

Types of Client Scripts

  1. onLoad: Runs when form loads
  2. onChange: Runs when a field value changes
  3. onSubmit: Runs before form submission

g_form Usage

g_form is the primary client-side API.

Common g_form functions:

  1. getValue()
  2. setValue()
  3. setMandatory()
  4. setDisplay()
  5. showFieldMsg()

Hands-On: Dynamic Field Visibility

  1. Navigate to System UI → Client Scripts
  2. Create an onChange client script
  3. Define condition based on field value
  4. Show or hide another field dynamically
  5. Save and test

Hands-On: Client-Side Validation

  1. Create an onSubmit client script
  2. Validate mandatory conditions
  3. Display error message if validation fails
  4. Prevent form submission
  5. Save and test

Script Includes

Overview

Script Includes store reusable server-side JavaScript code.

They are used to:

  1. Avoid duplicate logic
  2. Improve maintainability
  3. Support modular design

Reusable Server-Side Code

Script Includes can:

  1. Query multiple tables
  2. Perform complex logic
  3. Return results to other scripts

AJAX Calls

Client scripts can call Script Includes using GlideAjax.

Use cases:

  1. Fetch data dynamically
  2. Validate data without form reload

Best Practices

  1. Mark Script Includes as Client Callable only when required
  2. Use descriptive names
  3. Keep logic reusable and generic
  4. Avoid UI logic in Script Includes

Hands-On: Server-Side Utility Scripts

  1. Navigate to System Definition → Script Includes
  2. Create a new Script Include
  3. Write reusable server-side function
  4. Call it from Business Rule or Client Script
  5. Test functionality

Completion Outcome

After completing this chapter, you will be able to:

  1. Write basic JavaScript in ServiceNow
  2. Use GlideRecord to interact with data
  3. Differentiate client-side and server-side scripts
  4. Create and optimize Business Rules
  5. Build Client Scripts using g_form
  6. Develop reusable Script Includes
  7. Implement validations and automation logic