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:
- Variables and data types
- Functions
- Conditions and loops
- 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:
- Query records
- Insert records
- Update records
- Delete records
Basic example:
- Query incidents where state is Open
- 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:
- Client Scripts
- UI Policies
Used for:
- Field validation
- UI behavior
- Dynamic form changes
Server-Side Scripting
Runs on the ServiceNow server.
Examples:
- Business Rules
- Script Includes
- Background Scripts
Used for:
- Data processing
- Security enforcement
- Database updates
Business Rules
Overview
Business Rules execute server-side logic when records are inserted, updated, deleted, or queried.
Types of Business Rules
- Before: Runs before record is saved
- After: Runs after record is saved
- Async: Runs in background
- Display: Runs when form is loaded
Use Cases
- Auto-assign incidents
- Set default values
- Prevent invalid updates
- Sync data between tables
Best Practices
- Use Before rules when possible
- Avoid unnecessary queries
- Keep scripts simple and readable
- Use conditions to limit execution
- Avoid using Business Rules for UI logic
Hands-On: Auto-Update Incident Fields
- Navigate to System Definition → Business Rules
- Create a new Business Rule on Incident table
- Set type as Before Update
- Add condition (for example: Priority changes)
- Write script to update another field
- Save and test
Hands-On: Enforce Validations
- Create a Before Insert or Before Update Business Rule
- Add logic to block record save
- Use error messages to inform user
- Test validation behavior
- Client Scripts
Overview
Client Scripts run in the browser and control form behavior.
Types of Client Scripts
- onLoad: Runs when form loads
- onChange: Runs when a field value changes
- onSubmit: Runs before form submission
g_form Usage
g_form is the primary client-side API.
Common g_form functions:
- getValue()
- setValue()
- setMandatory()
- setDisplay()
- showFieldMsg()
Hands-On: Dynamic Field Visibility
- Navigate to System UI → Client Scripts
- Create an onChange client script
- Define condition based on field value
- Show or hide another field dynamically
- Save and test
Hands-On: Client-Side Validation
- Create an onSubmit client script
- Validate mandatory conditions
- Display error message if validation fails
- Prevent form submission
- Save and test
Script Includes
Overview
Script Includes store reusable server-side JavaScript code.
They are used to:
- Avoid duplicate logic
- Improve maintainability
- Support modular design
Reusable Server-Side Code
Script Includes can:
- Query multiple tables
- Perform complex logic
- Return results to other scripts
AJAX Calls
Client scripts can call Script Includes using GlideAjax.
Use cases:
- Fetch data dynamically
- Validate data without form reload
Best Practices
- Mark Script Includes as Client Callable only when required
- Use descriptive names
- Keep logic reusable and generic
- Avoid UI logic in Script Includes
Hands-On: Server-Side Utility Scripts
- Navigate to System Definition → Script Includes
- Create a new Script Include
- Write reusable server-side function
- Call it from Business Rule or Client Script
- Test functionality
Completion Outcome
After completing this chapter, you will be able to:
- Write basic JavaScript in ServiceNow
- Use GlideRecord to interact with data
- Differentiate client-side and server-side scripts
- Create and optimize Business Rules
- Build Client Scripts using g_form
- Develop reusable Script Includes
- Implement validations and automation logic