mcpField
N
notifications

notifications

github.com/happy-technologies-llc开发4142 次安装2 天前更新
代码仓库

关于 notifications

这是一份用于设计和管理 ServiceNow 通知的开发技能,面向构建记录变更、审批、SLA 和业务事件提醒的团队。内容包括事件触发、模板、收件人逻辑、短信与推送选项,以及投递故障排查。

主要功能

  1. 01配置事件型和条件型通知
  2. 02创建可复用邮件模板
  3. 03设置静态与动态收件人
  4. 04配置短信和推送通知
  5. 05筛选、排序并排查提醒

适用场景

  1. 01在事件变更时通知负责人
  2. 02发送审批或 SLA 提醒
  3. 03构建动态收件人通知
  4. 04排查邮件投递失败

MCP 相关

技能示范如何使用 ServiceNow MCP 工具查询事件,以及创建或更新通知、模板和邮件脚本记录。

安装

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

$npx skills add https://github.com/happy-technologies-llc/happy-platform-skills --skill notifications

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

详细介绍

邮件通知开发

概述

本技能涵盖创建和管理 ServiceNow 邮件通知的完整流程:

  • 了解通知类型(基于事件与直接触发)
  • 使用恰当的触发条件创建邮件通知
  • 使用动态内容构建邮件模板
  • 编写邮件脚本以实现高级收件人逻辑
  • 配置 SMS 和推送通知
  • 排查投递问题

使用场景: 为记录变更、审批、SLA 超时、工作流活动或自定义业务事件构建自动通知。

前提条件

  • 角色: admin、notification_admin 或 itil(用于查看)
  • 访问权限: sysevent_email_action、sysevent_email_template、sys_script_email 表
  • 知识要求: GlideRecord 基础知识、邮件概念、事件注册表
  • 相关技能: 完成 admin/workflow-creation,以配置由工作流触发的通知

通知架构

核心表
sysevent_register (Event Registry)
    |
    +-- sysevent_email_action (Email Notifications)
            |
            +-- sysevent_email_template (Email Templates)
            +-- sys_script_email (Mail Scripts)
            +-- cmn_notif_message (Notification Messages)
通知类型
类型触发方式使用场景
基于事件系统/自定义事件标准方式——解耦且可复用
直接触发记录条件快速设置——与条件紧密耦合
工作流工作流活动工作流编排的一部分
脚本GlideEmailOutbound程序化方式——完全控制
关键表参考
表用途
sysevent_email_action邮件通知定义
sysevent_email_template邮件正文模板
sysevent_register事件定义
sys_script_email收件人邮件脚本
sys_email邮件日志(已发送/失败)
cmn_notif_message通知消息存储
cmn_notif_device用户通知设备

流程

第 1 步:了解基于事件的通知与直接触发的通知

基于事件的通知(推荐):

  • 由事件触发(例如,incident.assigned、change.approval.rejected)
  • 与业务规则解耦
  • 每个事件可以配置多个通知
  • 更适用于复杂且可复用的通知逻辑

直接触发的通知:

  • 直接由记录条件触发
  • 设置更简单
  • 与特定条件紧密耦合
  • 适用于一次性通知
第 2 步:查询现有事件

创建通知前,先了解可用事件。

查询事件注册表:

Tool: SN-Query-Table
Parameters:
  table_name: sysevent_register
  query: table=incident
  fields: sys_id,event_name,table,description,fired_by
  limit: 50

常见系统事件:

事件表触发条件
incident.assignedincident事件分配给用户/群组
incident.commentedincident添加评论
incident.resolvedincident事件已解决
incident.reopenedincident事件已重新打开
change.approval.rejectedchange_request变更审批被拒绝
change.itil.approval.rejectedchange_requestITIL 变更被拒绝
sc_request.approvedsc_request请求已批准
sla.warningtask_sla达到 SLA 警告阈值
sla.breachedtask_slaSLA 已超时
approval.insertedsysapproval_approver已创建审批记录
task.assignedtask任务已分配
第 3 步:创建自定义事件(可选)

如果没有现有事件符合需求,请创建自定义事件。

创建事件注册:

Tool: SN-Create-Record
Parameters:
  table_name: sysevent_register
  data:
    event_name: "x_custom.incident.escalated"
    table: "incident"
    description: "Fired when incident is escalated to management"
    fired_by: "Business Rule: Incident Escalation"

从业务规则触发事件:

// In business rule script
if (current.escalation.changes() && current.escalation > 0) {
    gs.eventQueue('x_custom.incident.escalated', current, current.assigned_to, current.assignment_group);
}

通过后台脚本触发事件:

Tool: SN-Execute-Background-Script
Parameters:
  script: |
    var inc = new GlideRecord('incident');
    inc.get('number', 'INC0010001');
    if (inc.isValidRecord()) {
        gs.eventQueue('x_custom.incident.escalated', inc, inc.assigned_to.toString(), inc.assignment_group.toString());
        gs.info('Event fired for: ' + inc.number);
    }
  description: "Fire custom escalation event for testing"
第 4 步:创建邮件通知

基于事件的基本通知:

Tool: SN-Create-Record
Parameters:
  table_name: sysevent_email_action
  data:
    name: "Incident Escalation Alert"
    event_name: "x_custom.incident.escalated"
    sys_domain: "global"
    collection: "incident"
    active: true
    # Subject
    subject: "URGENT: Incident ${number} Escalated - ${short_description}"
    # Recipients
    send_to_event_creator: false
    recipient_users: [user_sys_id]  # Specific users
    recipient_groups: [group_sys_id]  # Groups
    send_to_event_parm_1: true  # gs.eventQueue parm1 (assigned_to)
    send_to_event_parm_2: true  # gs.eventQueue parm2 (assignment_group)
    # Content
    content_type: "text/html"
    message_html: |
      <html>
      <body>
      <h2>Incident Escalated</h2>
      <p>The following incident has been escalated and requires immediate attention:</p>
      <table border="1" cellpadding="5">
        <tr><td><b>Number:</b></td><td>${number}</td></tr>
        <tr><td><b>Priority:</b></td><td>${priority}</td></tr>
        <tr><td><b>Description:</b></td><td>${short_description}</td></tr>
        <tr><td><b>Assigned To:</b></td><td>${assigned_to}</td></tr>
        <tr><td><b>Assignment Group:</b></td><td>${assignment_group}</td></tr>
        <tr><td><b>Escalation Level:</b></td><td>${escalation}</td></tr>
      </table>
      <p><a href="${URI_REF}">View Incident</a></p>
      </body>
      </html>

直接触发(基于条件)的通知:

Tool: SN-Create-Record
Parameters:
  table_name: sysevent_email_action
  data:
    name: "High Priority Incident Created"
    # No event_name - uses conditions instead
    sys_domain: "global"
    collection: "incident"
    action_insert: true
    action_update: false
    action_delete: false
    condition: "priority=1^state=1"  # P1, New state
    active: true
    subject: "P1 Incident Created: ${number}"
    send_to_event_parm_1: true
    content_type: "text/html"
    message_html: |
      <h2>Priority 1 Incident Created</h2>
      <p>A new Priority 1 incident requires immediate attention.</p>
      <p><b>Number:</b> ${number}</p>
      <p><b>Description:</b> ${short_description}</p>
      <p><a href="${URI_REF}">View Incident</a></p>
步骤 5:配置收件人

sysevent_email_action 中的收件人选项:

字段用途
recipient_users特定用户的 sys_id(以逗号分隔)
recipient_groups组的 sys_id — 向所有成员发送邮件
send_to_event_creator触发事件的用户
send_to_event_parm_1gs.eventQueue 中的第一个参数
send_to_event_parm_2gs.eventQueue 中的第二个参数
recipient_fields包含用户引用的记录字段
send_self发送给执行更改的用户
exclude_delegates跳过代理用户
email_template可复用模板的引用

基于字段的收件人:

Tool: SN-Update-Record
Parameters:
  table_name: sysevent_email_action
  sys_id: [notification_sys_id]
  data:
    recipient_fields: "assigned_to,opened_by,caller_id"  # Comma-separated fields

组收件人:

Tool: SN-Update-Record
Parameters:
  table_name: sysevent_email_action
  sys_id: [notification_sys_id]
  data:
    recipient_groups: [group1_sys_id],[group2_sys_id]
步骤 6:创建用于动态收件人的邮件脚本

邮件脚本支持超出简单字段引用的复杂收件人逻辑。

创建邮件脚本:

Tool: SN-Create-Record
Parameters:
  table_name: sys_script_email
  data:
    name: "Get On-Call Manager"
    description: "Returns the on-call manager for the assignment group"
    script: |
      // Mail script to find on-call manager
      // 'current' is the record, 'email' is the GlideEmailOutbound object

      (function runMailScript(current, template, email, email_action, event) {

        // Get on-call rotation for assignment group
        var oncall = new GlideRecord('on_call_rotation');
        oncall.addQuery('group', current.assignment_group);
        oncall.addQuery('active', true);
        oncall.query();

        if (oncall.next()) {
          // Add on-call user as recipient
          var user = new GlideRecord('sys_user');
          user.get(oncall.user);
          if (user.isValidRecord() && user.email) {
            email.addAddress('to', user.email, user.name);
            gs.info('Mail script: Added recipient ' + user.email);
          }
        }

        // Optionally add escalation manager
        if (current.escalation > 1) {
          var manager = current.assignment_group.manager;
          if (manager && manager.email) {
            email.addAddress('cc', manager.email, manager.name);
          }
        }

      })(current, template, email, email_action, event);

将邮件脚本关联到通知:

Tool: SN-Update-Record
Parameters:
  table_name: sysevent_email_action
  sys_id: [notification_sys_id]
  data:
    sys_script_mail: [mail_script_sys_id]

高级邮件脚本 — 管理者层级:

Tool: SN-Create-Record
Parameters:
  table_name: sys_script_email
  data:
    name: "Get Manager Chain"
    description: "Adds manager hierarchy up to specified levels"
    script: |
      (function runMailScript(current, template, email, email_action, event) {

        var MAX_LEVELS = 3;
        var userId = current.assigned_to.toString();

        for (var level = 0; level < MAX_LEVELS && userId; level++) {
          var user = new GlideRecord('sys_user');
          user.get(userId);

          if (user.isValidRecord() && user.manager) {
            var manager = new GlideRecord('sys_user');
            manager.get(user.manager);

            if (manager.isValidRecord() && manager.email) {
              email.addAddress('cc', manager.email, manager.name);
              userId = manager.sys_id.toString();
            } else {
              break;
            }
          } else {
            break;
          }
        }

      })(current, template, email, email_action, event);
步骤 7:创建邮件模板

邮件模板支持复用 HTML/文本格式。

创建邮件模板:

Tool: SN-Create-Record
Parameters:
  table_name: sysevent_email_template
  data:
    name: "Incident Alert Template"
    description: "Standard template for incident notifications"
    subject: "${number}: ${short_description}"
    message_html: |
      <!DOCTYPE html>
      <html>
      <head>
        <style>
          body { font-family: Arial, sans-serif; }
          .header { background-color: #1a73e8; color: white; padding: 20px; }
          .content { padding: 20px; }
          .details { border-collapse: collapse; width: 100%; }
          .details td { border: 1px solid #ddd; padding: 10px; }
          .label { background-color: #f5f5f5; font-weight: bold; width: 30%; }
          .footer { background-color: #f5f5f5; padding: 15px; font-size: 12px; }
          .button { background-color: #1a73e8; color: white; padding: 10px 20px; text-decoration: none; border-radius: 4px; }
        </style>
      </head>
      <body>
        <div class="header">
          <h1>Incident Alert</h1>
        </div>
        <div class="content">
          <p>An incident requires your attention:</p>
          <table class="details">
            <tr>
              <td class="label">Number</td>
              <td>${number}</td>
            </tr>
            <tr>
              <td class="label">Priority</td>
              <td>${priority}</td>
            </tr>
            <tr>
              <td class="label">State</td>
              <td>${state}</td>
            </tr>
            <tr>
              <td class="label">Description</td>
              <td>${short_description}</td>
            </tr>
            <tr>
              <td class="label">Assigned To</td>
              <td>${assigned_to}</td>
            </tr>
            <tr>
              <td class="label">Assignment Group</td>
              <td>${assignment_group}</td>
            </tr>
            <tr>
              <td class="label">Opened</td>
              <td>${opened_at}</td>
            </tr>
          </table>
          <p style="margin-top: 20px;">
            <a href="${URI_REF}" class="button">View Incident</a>
          </p>
        </div>
        <div class="footer">
          <p>This is an automated notification from ServiceNow.</p>
          <p>Do not reply to this email.</p>
        </div>
      </body>
      </html>
    message: |
      Incident Alert

      Number: ${number}
      Priority: ${priority}
      State: ${state}
      Description: ${short_description}
      Assigned To: ${assigned_to}
      Assignment Group: ${assignment_group}

      View Incident: ${URI_REF}

      This is an automated notification.
    collection: "incident"

将模板关联到通知:

Tool: SN-Update-Record
Parameters:
  table_name: sysevent_email_action
  sys_id: [notification_sys_id]
  data:
    email_template: [template_sys_id]
    # Clear inline content if using template
    message_html: ""
    message: ""
步骤 8:邮件变量语法

标准字段变量:

${field_name}            - Display value of field
${field_name.name}       - Reference field's name
${field_name.email}      - Reference field's email
${field_name.sys_id}     - Reference field's sys_id
${number}                - Incident number
${short_description}     - Short description
${priority}              - Priority display value
${state}                 - State display value

特殊变量:

${URI}                   - Record URI path only
${URI_REF}               - Full URL to record
${mailto:email}          - Mailto link
${event.parm1}           - First event parameter
${event.parm2}           - Second event parameter
${current}               - Current record reference
${gs.getProperty('name')} - System property value

条件内容(需要邮件脚本):

// In notification body script or template script
if (current.priority == 1) {
    template.print('<span style="color: red; font-weight: bold;">CRITICAL</span>');
} else {
    template.print('<span>' + current.priority.getDisplayValue() + '</span>');
}

遍历相关记录:

// Show related tasks in notification
template.print('<h3>Related Tasks:</h3>');
template.print('<ul>');

var tasks = new GlideRecord('sc_task');
tasks.addQuery('request_item', current.sys_id);
tasks.query();

while (tasks.next()) {
    template.print('<li>' + tasks.number + ' - ' + tasks.short_description + '</li>');
}

template.print('</ul>');
步骤 9:添加附件

包含记录附件:

Tool: SN-Update-Record
Parameters:
  table_name: sysevent_email_action
  sys_id: [notification_sys_id]
  data:
    include_attachments: true

通过邮件脚本以编程方式添加附件:

Tool: SN-Create-Record
Parameters:
  table_name: sys_script_email
  data:
    name: "Attach Report PDF"
    description: "Attaches a generated report to the email"
    script: |
      (function runMailScript(current, template, email, email_action, event) {

        // Get attachment from related record
        var attach = new GlideRecord('sys_attachment');
        attach.addQuery('table_name', 'incident');
        attach.addQuery('table_sys_id', current.sys_id);
        attach.addQuery('content_type', 'CONTAINS', 'pdf');
        attach.query();

        while (attach.next()) {
          // Add each PDF attachment
          var gsa = new GlideSysAttachment();
          var content = gsa.getContentBase64(attach);
          email.addAttachment(attach.file_name, attach.content_type, content);
        }

      })(current, template, email, email_action, event);
步骤 10:配置短信通知

短信通知需要短信插件和运营商配置。

检查短信功能:

Tool: SN-Query-Table
Parameters:
  table_name: sys_plugins
  query: name=com.snc.sms
  fields: sys_id,name,active

创建短信通知:

Tool: SN-Create-Record
Parameters:
  table_name: sysevent_email_action
  data:
    name: "P1 Incident SMS Alert"
    event_name: "incident.assigned"
    sys_domain: "global"
    collection: "incident"
    condition: "priority=1"
    active: true
    # SMS specific
    sms_alternate: true  # Enable SMS option
    subject: "P1: ${number}"  # SMS subject (carrier dependent)
    message: "P1 Incident ${number}: ${short_description}. Priority: ${priority}. View: ${URI_REF}"
    content_type: "text/plain"
    # Recipients
    send_to_event_parm_1: true

为用户配置短信:

Tool: SN-Query-Table
Parameters:
  table_name: cmn_notif_device
  query: user=[user_sys_id]^type=SMS
  fields: sys_id,phone_number,carrier,active
步骤 11:配置推送通知

推送通知需要移动端插件和设备注册。

创建推送通知:

Tool: SN-Create-Record
Parameters:
  table_name: sysevent_email_action
  data:
    name: "Approval Required Push"
    event_name: "approval.inserted"
    collection: "sysapproval_approver"
    active: true
    # Push specific
    push_message_only: true  # Send push only, no email
    subject: "Approval Required"
    message: "You have a new approval request: ${document_id.short_description}"
    # Recipients
    recipient_fields: "approver"

查询用户的推送设备:

Tool: SN-Query-Table
Parameters:
  table_name: cmn_notif_device
  query: user=[user_sys_id]^type=Push
  fields: sys_id,device_name,active,last_registration
步骤 12:通知条件和筛选

高级条件示例:

条件用途
priority=1^state!=6P1,尚未解决
active=true^assigned_toISEMPTY活跃、未分配
sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()今天创建
priority.changes()=true优先级字段已更改
state.changesFrom(1)^state.changesTo(2)状态从 New 更改为 In Progress

条件脚本(高级):

Tool: SN-Update-Record
Parameters:
  table_name: sysevent_email_action
  sys_id: [notification_sys_id]
  data:
    advanced_condition: |
      // Return true to send notification, false to skip
      function advancedCondition() {
        // Don't notify if CI is in maintenance window
        if (current.cmdb_ci && current.cmdb_ci.maintenance) {
          return false;
        }

        // Only notify during business hours
        var now = new GlideDateTime();
        var hour = now.getLocalTime().getHourOfDayLocalTime();
        if (hour < 8 || hour > 18) {
          return false;  // Outside business hours
        }

        // Check if user has opted out
        var prefs = new GlideRecord('sys_user_preference');
        prefs.addQuery('user', current.assigned_to);
        prefs.addQuery('name', 'notification.incident.optout');
        prefs.addQuery('value', 'true');
        prefs.query();
        if (prefs.next()) {
          return false;
        }

        return true;
      }

      advancedCondition();
步骤 13:权重和优先级

控制通知频率和重要性。

设置通知权重:

Tool: SN-Update-Record
Parameters:
  table_name: sysevent_email_action
  sys_id: [notification_sys_id]
  data:
    weight: 10  # Higher weight = higher priority
    digest: false  # true = batch into digest
    digest_interval: ""  # hourly, daily if digest=true
    omit_watermark: false  # Watermark prevents duplicates

摘要配置:

Tool: SN-Create-Record
Parameters:
  table_name: sysevent_email_action
  data:
    name: "Daily Incident Summary"
    event_name: "incident.created"
    collection: "incident"
    active: true
    digest: true
    digest_interval: "daily"
    digest_template: [digest_template_sys_id]
    subject: "Daily Incident Summary - ${digest.count} incidents"

邮件投递故障排除

步骤 1:检查邮件日志

查询最近的邮件:

Tool: SN-Query-Table
Parameters:
  table_name: sys_email
  query: type=send-ready^ORtype=sent^ORtype=send-error^sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()
  fields: sys_id,type,recipients,subject,sys_created_on,error,mailbox
  limit: 50
  orderBy: sys_created_on
  orderByDesc: true

邮件状态类型:

类型含义
send-ready已排队等待发送
sent已成功发送
send-error发送失败
received收件邮件
received-error收件处理错误
draft等待审批
ignored已按规则跳过

查找发送失败的邮件:

Tool: SN-Query-Table
Parameters:
  table_name: sys_email
  query: type=send-error^sys_created_onONLast 7 days@javascript:gs.daysAgoStart(7)@javascript:gs.daysAgoEnd(0)
  fields: sys_id,recipients,subject,error,sys_created_on
  limit: 100
步骤 2:检查事件队列

查询事件队列:

Tool: SN-Query-Table
Parameters:
  table_name: sysevent
  query: name=x_custom.incident.escalated^sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()
  fields: sys_id,name,instance,parm1,parm2,state,sys_created_on
  limit: 50

事件状态:

状态含义
ready已排队等待处理
processed已成功处理
error处理失败
步骤 3:验证通知配置

检查通知是否处于活动状态:

Tool: SN-Query-Table
Parameters:
  table_name: sysevent_email_action
  query: sys_id=[notification_sys_id]
  fields: name,active,event_name,condition,recipient_users,recipient_groups,recipient_fields

验证事件注册:

Tool: SN-Query-Table
Parameters:
  table_name: sysevent_register
  query: event_name=[your_event_name]
  fields: sys_id,event_name,table,description
步骤 4:测试通知

触发测试事件:

Tool: SN-Execute-Background-Script
Parameters:
  script: |
    // Test notification by firing event
    var inc = new GlideRecord('incident');
    inc.addQuery('active', true);
    inc.setLimit(1);
    inc.query();
    if (inc.next()) {
        gs.eventQueue('x_custom.incident.escalated', inc, inc.assigned_to.toString(), inc.assignment_group.toString());
        gs.info('Test event fired for: ' + inc.number);
    }
  description: "Fire test notification event"

检查邮件生成情况:

Tool: SN-Query-Table
Parameters:
  table_name: sys_email
  query: recipients=[test_email]^ORsubjectLIKE[incident_number]^sys_created_onONLast hour@javascript:gs.hoursAgoStart(1)@javascript:gs.hoursAgoEnd(0)
  fields: sys_id,type,recipients,subject,body,error
常见问题
通知未触发

症状: 更改记录后未生成邮件 原因:

  1. 通知未激活
  2. 条件不匹配
  3. 事件未注册
  4. 未定义收件人

解决方案:

Tool: SN-Query-Table
Parameters:
  table_name: sysevent_email_action
  query: collection=incident^active=true
  fields: name,event_name,condition,active,recipient_users,recipient_groups
收件人未收到电子邮件

**症状:**电子邮件显示已发送,但用户未收到 原因:

  1. 用户的电子邮件地址无效或缺失
  2. 用户已禁用通知偏好设置
  3. 电子邮件被垃圾邮件过滤器拦截
  4. 收件人配置不正确

解决方案:

Tool: SN-Query-Table
Parameters:
  table_name: sys_user
  query: sys_id=[user_sys_id]
  fields: email,notification,active

检查用户的通知偏好设置:

Tool: SN-Query-Table
Parameters:
  table_name: cmn_notif_message
  query: user=[user_sys_id]
  fields: notification,device,state
电子邮件显示为 send-error

**症状:**电子邮件发送失败,状态显示为错误 原因:

  1. SMTP 服务配置
  2. 电子邮件地址无效
  3. 邮件服务拒绝发送
  4. 附件过大

**解决方案:**检查 sys_email 记录中的 error 字段,查看具体错误信息。

变量未解析

**症状:**电子邮件中显示 ${field_name} 字面内容,而非对应的值 原因:

  1. 变量语法不正确
  2. 表中不存在该字段
  3. 字段值为 null 或为空

**解决方案:**验证字段是否存在:

Tool: SN-Get-Table-Schema
Parameters:
  table_name: incident

工具使用摘要

MCP 工具(首选)
工具用途
SN-Query-Table查询通知、事件和电子邮件日志
SN-Create-Record创建通知、模板和邮件脚本
SN-Update-Record修改通知配置
SN-Get-Table-Schema验证变量可用字段
SN-Execute-Background-Script触发测试事件、进行调试
REST API(替代方案)
端点方法用途
/api/now/table/sysevent_email_actionGET/POST查询/创建通知
/api/now/table/sysevent_email_templateGET/POST查询/创建模板
/api/now/table/sys_script_emailPOST创建邮件脚本
/api/now/table/sys_emailGET检查电子邮件日志
/api/now/table/sysevent_registerGET/POST管理事件

最佳实践

  • **使用事件:**优先使用基于事件的通知,而非直接通知,以实现解耦
  • **重用模板:**创建模板,确保各通知格式一致
  • **在非生产环境测试:**务必先在开发/测试环境中测试通知
  • **监控电子邮件队列:**定期检查 sys_email 中的 send-error 记录
  • **用户偏好设置:**尊重用户选择退出通知的设置
  • **营业时间:**对于非关键警报,应考虑时区和营业时间
  • **HTML + 纯文本:**同时提供两种格式,以实现最大兼容性
  • **避免垃圾邮件:**对于高频事件,使用摘要
  • **水印:**保持启用水印,以防止重复通知
  • **记录事件文档:**为自定义事件添加说明,便于维护

示例

示例 1:完整的 P1 事件通知
# 1. Register custom event
Tool: SN-Create-Record
Parameters:
  table_name: sysevent_register
  data:
    event_name: "incident.p1.created"
    table: "incident"
    description: "Fired when Priority 1 incident is created"

# 2. Create notification
Tool: SN-Create-Record
Parameters:
  table_name: sysevent_email_action
  data:
    name: "P1 Incident Alert - All Hands"
    event_name: "incident.p1.created"
    collection: "incident"
    active: true
    weight: 100
    subject: "[URGENT] P1 Incident: ${number} - ${short_description}"
    content_type: "text/html"
    message_html: |
      <h1 style="color: #d32f2f;">Priority 1 Incident Created</h1>
      <p>A critical incident requires immediate attention.</p>
      <table>
        <tr><td><b>Number:</b></td><td>${number}</td></tr>
        <tr><td><b>Description:</b></td><td>${short_description}</td></tr>
        <tr><td><b>Caller:</b></td><td>${caller_id}</td></tr>
        <tr><td><b>Assignment Group:</b></td><td>${assignment_group}</td></tr>
      </table>
      <p><a href="${URI_REF}">View Incident Now</a></p>
    recipient_groups: [incident_managers_group_sys_id]
    send_to_event_parm_1: true
    send_to_event_parm_2: true

# 3. Create business rule to fire event
# (Create via UI or background script)
示例 2:使用邮件脚本的审批请求通知
# 1. Create mail script for manager chain
Tool: SN-Create-Record
Parameters:
  table_name: sys_script_email
  data:
    name: "CC Manager Chain"
    script: |
      (function runMailScript(current, template, email, email_action, event) {
        // Get requested_for user
        var reqFor = current.document_id.requested_for;
        if (reqFor) {
          var user = new GlideRecord('sys_user');
          user.get(reqFor);

          // Add manager as CC
          if (user.manager && user.manager.email) {
            email.addAddress('cc', user.manager.email.toString(), user.manager.name.toString());
          }
        }
      })(current, template, email, email_action, event);

# 2. Create notification with mail script
Tool: SN-Create-Record
Parameters:
  table_name: sysevent_email_action
  data:
    name: "Approval Request - With Manager CC"
    event_name: "approval.inserted"
    collection: "sysapproval_approver"
    active: true
    subject: "Approval Required: ${document_id.number}"
    recipient_fields: "approver"
    sys_script_mail: [mail_script_sys_id]
    message_html: |
      <h2>Approval Request</h2>
      <p>You have a pending approval request.</p>
      <p><b>Request:</b> ${document_id.number}</p>
      <p><b>Requested By:</b> ${document_id.requested_for}</p>
      <p><a href="${URI_REF}">Approve/Reject</a></p>
示例 3:SLA 违约警告
Tool: SN-Create-Record
Parameters:
  table_name: sysevent_email_action
  data:
    name: "SLA Warning - 75% Elapsed"
    event_name: "sla.warning"
    collection: "task_sla"
    active: true
    condition: "percentage>=75^percentage<100"
    subject: "SLA Warning: ${task.number} at ${percentage}%"
    recipient_fields: "task.assigned_to"
    content_type: "text/html"
    message_html: |
      <h2 style="color: #ff9800;">SLA Warning</h2>
      <p>The following task is approaching SLA breach:</p>
      <table>
        <tr><td><b>Task:</b></td><td>${task.number}</td></tr>
        <tr><td><b>SLA:</b></td><td>${sla.name}</td></tr>
        <tr><td><b>Percentage:</b></td><td>${percentage}%</td></tr>
        <tr><td><b>Time Remaining:</b></td><td>${business_time_left}</td></tr>
      </table>
      <p><a href="${task.URI_REF}">View Task</a></p>

相关技能

  • admin/workflow-creation - 工作流触发的通知
  • catalog/approval-workflows - 审批通知
  • itsm/incident-lifecycle - 事件通知触发器
  • admin/script-execution - 通过脚本测试通知

参考资料

评论

支持 Markdown