BusinessRule
BusinessRule(config)
Creates a Business Rule: logic that runs when database records are queried, updated,
inserted, or deleted (sys_script).
Usage
BusinessRule({
$id: Now.ID['br0'],
when: 'after',
action: ['update'],
table: 'incident',
name: 'LogStateChange',
order: 100,
})
Parameters
config
BusinessRule<keyof Tables>
Configuration for the business rule
Properties:
-
$id (required):
string | number | ExplicitKey<string> -
name (required):
stringThe name of the business rule -
table (required):
keyof TablesThe table on which the business rule runs -
$meta (optional):
object- installMethod:
'first install' | 'demo'Map a record to an output folder that loads only in specific circumstances. 'first install' - > 'unload', 'demo' -> 'unload.demo'
- installMethod:
-
abortAction (optional):
booleanWhether to abort the current database transaction -
access (optional):
'' | 'public' | 'package_private'Application access level for cross-scope usage.- '': Platform default (same as package_private)
- 'package_private': Accessible only within this application scope
- 'public': Accessible from all application scopes
-
action (optional):
('update' | 'delete' | 'query' | 'insert')[]The database operations that trigger this business rule -
active (optional):
booleanWhether the business rule is enabled -
addMessage (optional):
booleanWhether to display a message when the business rule runs -
changeFields (optional):
booleanUpdate reference fields. Default: false -
clientCallable (optional):
booleanIs this rule callable by client scripts? Default: false -
condition (optional):
stringScript-based condition that must evaluate to true for the business rule to run -
description (optional):
stringDescription of the business rule's purpose -
executeFunction (optional):
booleanAutomatically execute the function predefined for this type of business rule (onBefore, onAfter, etc). Default: false -
filterCondition (optional):
stringCondition-builder conditions that must be met for the business rule to run -
message (optional):
stringThe message to display when add_message is true -
order (optional):
numberThe execution order of the business rule (lower numbers execute first) -
priority (optional):
numberSets priority value for async rules -
protectionPolicy (optional):
'read' | 'protected'The policy determines whether someone can view or edit the business rule after the application is installed on their instance.- read: Allow other application developers to see your script logic, but not to change it.
- protected: Prevent other application developers from changing this business rule.
- undefined Allow other application developers to customize your business rule.
-
rest (optional):
objectREST properties for rule-
method:
string | Record<'sys_rest_message_fn'>Reference to a HTTP Method -
methodText:
string -
service:
string | Record<'sys_rest_message'>Reference to a REST Message -
serviceText:
string -
variables:
stringRest Variables -
webService:
boolean(is_rest) Whether this rule is associated with an outbound REST. Default: false
-
-
roleConditions (optional):
(string | Role)[]Roles that the user must have for the business rule to execute -
script (optional):
string | (current: any, previous: any, dependencies: any[]) => voidThe script to execute, receives current and previous GlideRecords as arguments. -
setFieldValue (optional):
stringField values to set in the current record -
when (optional):
'before' | 'after' | 'async' | 'display' | 'async_deprecated'When the business rule should be executed relative to the database transaction
See
Examples
Business Rule with External Script (Module)
Create a business rule that uses an external JavaScript file for the script logic. In this example, we are using a modern modular JavaScript file, importing the function using a regular import statement.
import { BusinessRule } from '@servicenow/sdk/core'
import { logStateChange } from '../../server/business-rules/businessLogic'
export const LogIncidentStateChange = BusinessRule({
$id: Now.ID['log_state_change'],
name: 'Log State Change',
table: 'incident',
when: 'after',
action: ['update'],
script: logStateChange,
})
businessLogic.ts
import { gs } from '@servicenow/glide'
export function logStateChange(current: any, previous: any) {
const title = current.getValue('title')
const currentState = current.getValue('state')
const previousState = previous.getValue('state')
gs.addInfoMessage(`"${title}" updated from "${previousState}" to "${currentState}"`)
}
Business Rule with External Script (Non-modular, using Now.include)
Create a business rule that uses an external JavaScript file for the script logic. In this example, we are not using modern modular JavaScript. Instead, it's written as a plain legacy script without imports or exports, and we are using Now.include() to drop it into the business rule.
import { BusinessRule } from '@servicenow/sdk/core'
export const LogIncidentStateChange = BusinessRule({
$id: Now.ID['log_state_change'],
name: 'Log State Change',
table: 'incident',
when: 'after',
action: ['update'],
script: Now.include('../../server/business-rules/businessLogic.server.js'),
})
businessLogic.server.js
var title = current.getValue('title');
var currentState = current.getValue('state');
var previousState = previous.getValue('state');
gs.addInfoMessage(`"${title}" updated from "${previousState}" to "${currentState}"`);
For guidance on business rule types, execution order, and common patterns, see the business-rule-guide topic.