Scripting
What is a Business Rule in ServiceNow?
The question
What is a Business Rule in ServiceNow? Explain the different types (before, after, async, display) and when each one fires.
What the Interviewer Wants to Hear
Business Rules are the most important server-side automation in ServiceNow. Every developer and admin needs to know the four types and their execution timing.
What Is a Business Rule?
A Business Rule is a server-side script that runs automatically when a record is displayed, inserted, updated, deleted, or queried. Think of it as an automatic trigger — you define WHEN it fires and WHAT it does.
The Four Types
- Before Business Rule: Fires BEFORE the record is saved to the database. Use it to validate data, modify field values, or abort the save entirely.
// Example: Auto-set priority based on impact and urgency (function executeRule(current, previous) { if (current.impact == 1 && current.urgency == 1) { current.priority = 1; // Critical } })(current, previous); - After Business Rule: Fires AFTER the record is successfully saved. The database write is complete. Use it for things that depend on the record existing — sending notifications, creating related records, updating other tables.
// Example: Create a task when a change request is approved (function executeRule(current, previous) { if (current.approval.changesTo('approved')) { // Use internal value; 'approved' is the value for the approval field var task = new GlideRecord('change_task'); task.initialize(); task.change_request = current.sys_id; task.short_description = 'Implement: ' + current.short_description; task.insert(); } })(current, previous); - Async (Asynchronous) Business Rule: Fires AFTER the save but runs in the background on a separate thread. The user doesn't wait for it to finish. Use it for heavy operations — external API calls, complex calculations, bulk updates.
// Example: Sync incident data to an external monitoring system (function executeRule(current, previous) { var rm = new sn_ws.RESTMessageV2('MonitoringSystem', 'POST'); rm.setStringParameterNoEscape('incident_id', current.number); rm.execute(); // Runs in background — user doesn't wait })(current, previous); - Display Business Rule: Fires when a form is loaded (displayed) to the user. Does NOT run on list views, imports, or web service calls. Use it to calculate display-only values or pass data to client scripts via the
g_scratchpadobject.// Example: Show a warning message when a VIP's incident is opened (function executeRule(current, previous) { if (current.caller_id.vip == true) { g_scratchpad.isVIP = true; // Pass to client script } })(current, previous);
Execution Order
// When a user clicks 'Save' on an incident form: // 1. Client Script (onSubmit) — browser validates // 2. Data sent to server // 3. BEFORE Business Rules — server validates/modifies // 4. DATABASE WRITE — record saved // 5. AFTER Business Rules — post-save logic // 6. ASYNC Business Rules — queued for background execution // // When a user OPENS a form: // 1. Database read // 2. DISPLAY Business Rules — prepare display data // 3. Form renders in browser // 4. Client Script (onLoad) — browser-side setup
💡 Interview Tip
The follow-up question is always: 'Can you access the previous object in an async Business Rule?' The answer is no — async BRs don't have access to the previous values because they run outside the original transaction. If you need to compare old vs new values, do it in a before/after BR and store the result.