AgentSkillsCN

servicenow-business-rules

创建具备恰当执行时机、防止递归调用以及验证规则的服务器端数据库触发器。当您需要实现 Before/After/Async 业务规则、防止规则递归执行,或在数据库操作中强制执行数据校验时,可使用此技能。

SKILL.md
--- frontmatter
name: servicenow-business-rules
description: Create server-side database triggers with proper execution timing, recursion prevention, and validation patterns. Use when implementing Before/After/Async business rules, preventing recursive rule execution, or enforcing data validation on database operations.

ServiceNow Business Rules

Quick start

Basic wrapping pattern (prevents global variable pollution):

javascript
(function executeRule(current, previous) {
    // Your code here
})(current, previous);

Execution timing:

TypeUse CaseKey Constraint
BeforeModifying current record before saveDO NOT use current.update()
AfterUpdating related records immediatelyDO NOT modify current directly
AsyncIntegrations, heavy calculationsRuns in background
DisplayPass data to g_scratchpadRead-only access

Preventing recursion:

javascript
// ✓ CORRECT: Direct field assignment in Before rule
current.state = 3; // Auto-saved

// ✗ WRONG: Never use update() in Before/After
current.update(); // Causes loop

Validation pattern

javascript
if (current.u_start_date > current.u_end_date) {
    gs.addErrorMessage('Start date must be before end date');
    current.setAbortAction(true); // Blocks DB transaction
}

Best practices

  • Use IIFE wrapper for all rules
  • Check execution timing before using current.update()
  • Refactor complex logic (>10 lines) into Script Includes
  • Test recursion prevention on sub-production first
  • Use Condition builder in the UI when possible

Reference

For complete execution matrix and advanced patterns, see BEST_PRACTICES.md