mcpField
N
notification-events

notification-events

github.com/serac-labs开发7761 次安装9 周前更新
代码仓库

关于 notification-events

面向 ServiceNow 开发人员的通知事件技能,说明事件注册、排队、脚本动作、邮件通知和队列调试,并提供 ES5 示例。

主要功能

  1. 01注册并排队事件
  2. 02配置事件脚本动作
  3. 03创建条件邮件通知
  4. 04安排延迟与提醒事件
  5. 05查询待处理事件

适用场景

  1. 01事件发生时触发脚本或通知
  2. 02安排延迟提醒
  3. 03将批处理任务放入事件队列
  4. 04检查事件队列中的待处理项

MCP 相关

文档列出用于排队事件、查询事件队列、查找事件配置和测试事件脚本的 MCP 工具。

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/serac-labs/serac --skill notification-events

安装后在 Claude Code 中输入/notification-events确认已加载。

详细介绍

ServiceNow 的通知事件

事件会在 ServiceNow 中触发通知、脚本和工作流。

事件架构

gs.eventQueue() → Event Queue (sysevent)
    ↓
Event Registry (sysevent_register)
    ↓
Script Actions (sysevent_script_action)
Notifications (sysevent_email_action)

关键表

表用途
sysevent事件队列
sysevent_register事件注册表
sysevent_script_action脚本操作
sysevent_email_action电子邮件通知

创建事件(ES5)

注册事件
// Register event in sysevent_register (ES5 ONLY!)
var event = new GlideRecord("sysevent_register")
event.initialize()

event.setValue("event_name", "x_myapp.incident.escalated")
event.setValue("table", "incident")
event.setValue("description", "Fired when an incident is escalated")
event.setValue("fired_by", "Business Rules, Scripts")

// Parameters
event.setValue("parm1", "escalation_level")
event.setValue("parm2", "previous_group")

event.insert()
将事件加入队列
// Queue event from script (ES5 ONLY!)
// gs.eventQueue(event_name, gr, parm1, parm2)

// In a Business Rule
;(function executeRule(current, previous) {
  // Check if escalated
  if (current.escalation.changesTo("1")) {
    gs.eventQueue(
      "x_myapp.incident.escalated",
      current,
      current.getValue("escalation"),
      previous.assignment_group.getDisplayValue(),
    )
  }
})(current, previous)
事件参数
// Access event parameters in script action (ES5 ONLY!)
// event.parm1 = first parameter
// event.parm2 = second parameter

// In Script Action
;(function executeEvent(event) {
  var escalationLevel = event.parm1
  var previousGroup = event.parm2

  var incident = event.getGlideRecord()

  gs.info(
    "Incident " + incident.getValue("number") + " escalated to level " + escalationLevel + " from " + previousGroup,
  )

  // Perform action based on event
  if (escalationLevel === "1") {
    notifyManager(incident)
  } else if (escalationLevel === "2") {
    notifyDirector(incident)
  } else if (escalationLevel === "3") {
    notifyVP(incident)
  }
})(event)

脚本操作(ES5)

创建脚本操作
// Create script action for event (ES5 ONLY!)
var action = new GlideRecord("sysevent_script_action")
action.initialize()

action.setValue("name", "Handle Incident Escalation")
action.setValue("event_name", "x_myapp.incident.escalated")
action.setValue("active", true)

// Condition (optional)
action.setValue("condition", "event.parm1 >= 2")

// Script (ES5 ONLY!)
action.setValue(
  "script",
  "(function executeEvent(event) {\n" +
    "    var incident = event.getGlideRecord();\n" +
    "    var escalationLevel = event.parm1;\n" +
    "    \n" +
    "    // Create escalation task\n" +
    '    var task = new GlideRecord("task");\n' +
    "    task.initialize();\n" +
    "    task.parent = incident.getUniqueValue();\n" +
    '    task.short_description = "Review escalated incident";\n' +
    "    task.assignment_group = getEscalationGroup(escalationLevel);\n" +
    "    task.insert();\n" +
    "    \n" +
    '    gs.info("Created escalation task for " + incident.number);\n' +
    "})(event);",
)

action.insert()
延迟事件
// Queue event with delay (ES5 ONLY!)
// Will be processed after specified time

var delay = new GlideDateTime()
delay.addSeconds(3600) // 1 hour delay

var event = new GlideRecord("sysevent")
event.initialize()
event.setValue("name", "x_myapp.reminder.send")
event.setValue("instance", recordSysId)
event.setValue("table", "incident")
event.setValue("parm1", "first_reminder")
event.setValue("parm2", "")
event.setValue("claimed", false)
event.setValue("process_on", delay)
event.insert()

电子邮件通知(ES5)

创建电子邮件操作
// Create notification for event (ES5 ONLY!)
var notification = new GlideRecord("sysevent_email_action")
notification.initialize()

notification.setValue("name", "Incident Escalation Notification")
notification.setValue("event_name", "x_myapp.incident.escalated")
notification.setValue("active", true)

// Recipients
notification.setValue("recipient_users", "")
notification.setValue("recipient_groups", getGroupSysId("IT Management"))
notification.setValue("send_self", false)

// Use event.parm1 to get escalation manager
notification.setValue("recipient_fields", "assignment_group.manager")

// Email content
notification.setValue("subject", "Incident ${number} Escalated - Level ${event.parm1}")
notification.setValue(
  "message_html",
  "<p>Incident <b>${number}</b> has been escalated.</p>" +
    "<p>Short Description: ${short_description}</p>" +
    "<p>Escalation Level: ${event.parm1}</p>" +
    "<p>Previous Group: ${event.parm2}</p>" +
    '<p><a href="${URI_REF}">View Incident</a></p>',
)

notification.insert()
条件通知
// Notification with advanced condition (ES5 ONLY!)
var notification = new GlideRecord("sysevent_email_action")
notification.initialize()
notification.setValue("name", "VIP Incident Alert")
notification.setValue("event_name", "incident.created")

// Advanced condition script
notification.setValue("advanced_condition", true)
notification.setValue("condition", "current.caller_id.vip == true && current.priority <= 2")

// Send to specific recipients for VIP
notification.setValue("recipient_groups", getGroupSysId("VIP Support"))

notification.insert()

常见事件模式(ES5)

提醒事件
// Schedule reminder events (ES5 ONLY!)
function scheduleReminder(tableName, recordSysId, reminderType, delayMinutes) {
  var eventTime = new GlideDateTime()
  eventTime.addSeconds(delayMinutes * 60)

  var reminder = new GlideRecord("sysevent")
  reminder.initialize()
  reminder.setValue("name", "x_myapp.reminder")
  reminder.setValue("instance", recordSysId)
  reminder.setValue("table", tableName)
  reminder.setValue("parm1", reminderType)
  reminder.setValue("process_on", eventTime)
  reminder.insert()

  return reminder.getUniqueValue()
}

// Cancel scheduled reminder
function cancelReminder(eventSysId) {
  var reminder = new GlideRecord("sysevent")
  if (reminder.get(eventSysId)) {
    reminder.deleteRecord()
    return true
  }
  return false
}
批处理事件
// Queue batch processing (ES5 ONLY!)
// Business Rule: after, insert, u_import_batch

;(function executeRule(current, previous) {
  // Queue processing for each record in batch
  var record = new GlideRecord("u_import_record")
  record.addQuery("batch", current.getUniqueValue())
  record.query()

  while (record.next()) {
    gs.eventQueue("x_myapp.import.process_record", record, current.getUniqueValue(), "")
  }

  // Queue completion check
  var delay = new GlideDateTime()
  delay.addSeconds(300) // Check in 5 minutes

  var event = new GlideRecord("sysevent")
  event.initialize()
  event.setValue("name", "x_myapp.import.check_complete")
  event.setValue("instance", current.getUniqueValue())
  event.setValue("table", "u_import_batch")
  event.setValue("process_on", delay)
  event.insert()
})(current, previous)
状态变更事件
// Generic state change event (ES5 ONLY!)
// Business Rule: after, update

;(function executeRule(current, previous) {
  if (current.state.changes()) {
    gs.eventQueue(
      "x_myapp." + current.getTableName() + ".state_change",
      current,
      previous.getValue("state"),
      current.getValue("state"),
    )
  }
})(current, previous)

事件调试(ES5)

检查事件队列
// Query pending events (ES5 ONLY!)
function getPendingEvents(eventName) {
  var events = []

  var event = new GlideRecord("sysevent")
  event.addQuery("name", eventName)
  event.addQuery("claimed", false)
  event.orderByDesc("sys_created_on")
  event.setLimit(100)
  event.query()

  while (event.next()) {
    events.push({
      sys_id: event.getUniqueValue(),
      name: event.getValue("name"),
      instance: event.getValue("instance"),
      parm1: event.getValue("parm1"),
      parm2: event.getValue("parm2"),
      process_on: event.getValue("process_on"),
      created: event.getValue("sys_created_on"),
    })
  }

  return events
}

MCP 工具集成

可用工具
工具用途
snow_create_event将事件加入队列
snow_query_table查询事件队列
snow_artifact_manage (action='find')查找事件配置
snow_execute_script测试事件脚本
工作流示例
// 1. Queue an event
await snow_create_event({
  name: "x_myapp.test.event",
  table: "incident",
  instance: incidentSysId,
  parm1: "test_value",
})

// 2. Check event queue
await snow_query_table({
  table: "sysevent",
  query: "name=x_myapp.test.event^claimed=false",
  fields: "name,instance,parm1,parm2,process_on",
})

// 3. Find script actions
await snow_query_table({
  table: "sysevent_script_action",
  query: "event_nameLIKEx_myapp",
  fields: "name,event_name,active,condition",
})

最佳实践

  1. 为事件添加命名空间 - 使用应用前缀(x_myapp.*)
  2. 注册事件 - 在 sysevent_register 中记录
  3. 使用有意义的名称 - 清晰说明事件用途
  4. 参数 - 合理使用 parm1/parm2
  5. 条件 - 在处理前进行筛选
  6. 异步处理 - 不要阻塞事务
  7. 错误处理 - 处理脚本故障
  8. 仅使用 ES5 - 不使用现代 JavaScript 语法

评论

支持 Markdown