Introduction
ServiceNow is a powerful platform that enables organizations to streamline IT service management, automate workflows, and enhance business operations. One of the key features of ServiceNow is its scripting capabilities, which allow developers to customize and extend the platform’s functionalities. If you’re new to ServiceNow scripting, this guide will help you write your first script step by step.
Understanding ServiceNow Scripting
ServiceNow supports scripting in JavaScript, primarily using server-side (Business Rules, Script Includes, Scheduled Jobs) and client-side (Client Scripts, UI Policies) scripts. Knowing where to place your script depends on whether you want to process data on the client side (browser) or the server side (ServiceNow instance).
Types of Scripts in ServiceNow
- Client Scripts – Run on the user’s browser to provide real-time form interactions.
- Business Rules – Run on the server to automate tasks and enforce policies.
- Script Includes – Reusable server-side scripts that can be called from other scripts.
- UI Actions – Custom buttons or links that execute scripts.
- Scheduled Jobs – Run scripts at scheduled intervals.
Writing Your First Script: A Simple Client Script
Let’s start with a Client Script to auto-fill a field when another field changes.
Steps to Create a Client Script
- Navigate to Client Scripts:
- In the left navigation pane, go to System Definition > Client Scripts.
- Click ‘New’ to create a new client script.
- Fill in the details:
- Name: Auto-fill Location
- Table: Incident
- Type: onChange (Executes when a field changes)
- Field Name: Caller (Executes when the caller field changes)
- Write the Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Fetch the user's location and auto-fill the Location field
var user = g_form.getReference('caller_id', function(userRecord) {
if (userRecord) {
g_form.setValue('location', userRecord.location);
}
});
}
- Save and test the script by selecting a caller on an Incident form. The location field should auto-fill based on the selected caller.
Writing a Simple Business Rule
Now, let’s write a Business Rule to set a default priority when an incident is created.
Steps to Create a Business Rule
- Navigate to Business Rules:
- Go to System Definition > Business Rules.
- Click ‘New’ to create a new business rule.
- Fill in the details:
- Name: Set Default Priority
- Table: Incident
- When: Before Insert (Runs before a new record is inserted)
- Write the Script:
(function executeRule(current, gForm, gSNC) {
if (!current.priority) {
current.priority = 3; // Setting default priority to 3
}
})(current, gForm, gSNC);
- Save and test by creating a new Incident. If no priority is set, it should default to 3.
Best Practices for ServiceNow Scripting
- Use the Glide API: ServiceNow provides the
GlideRecord
,GlideSystem
, and other APIs to interact with records efficiently. - Optimize Performance: Avoid unnecessary database queries and loops.
- Follow Naming Conventions: Use meaningful names for scripts and functions.
- Test Scripts in a Sandbox: Always test in a non-production environment before deploying.
Conclusion
ServiceNow scripting is a powerful way to customize the platform to meet business needs. By starting with simple scripts like Client Scripts and Business Rules, you can automate tasks and improve workflows. As you gain more experience, you can explore advanced scripting techniques using Script Includes, Scheduled Jobs, and UI Actions. Happy coding!
Do you have any ServiceNow scripting questions? Drop them in the comments below!