mcpField
E
email-notifications

email-notifications

github.com/serac-labs開発7794 回インストール9 週間前に更新
リポジトリ

email-notifications について

ServiceNowの管理者や開発者向けに、イベント、条件、テンプレート、スクリプトを使った通知設定を扱うスキルです。宛先、ダイジェスト、通知の優先度、テスト、トラブルシューティングも説明します。

主な機能

  1. 01通知条件とイベントの設定
  2. 02メールテンプレートとスクリプトの作成
  3. 03動的な宛先の設定
  4. 04ダイジェストと優先度の設定
  5. 05送信のテストと問題調査

活用シーン

  1. 01レコードの割り当てをユーザーに通知
  2. 02エスカレーションや承認の通知
  3. 03日次インシデントダイジェストの作成
  4. 04宛先や内容の誤りの調査

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/serac-labs/serac --skill email-notifications

インストール後、Claude Code で/email-notificationsを入力して読み込まれたか確認します。

詳細

ServiceNow のメール通知

ServiceNow の通知はイベントによってトリガーされ、ユーザーにメール、SMS、その他のアラートを送信します。

通知のコンポーネント

コンポーネントテーブル目的
通知sysevent_email_actionメインの通知レコード
メールテンプレートsysevent_email_template再利用可能なメールレイアウト
イベントsysevent通知をトリガーします
イベント登録sysevent_registerカスタムイベントを定義します
メールスクリプトsys_script_email動的コンテンツのスクリプト

通知の作成

基本的な通知の構造
Notification: Incident Assigned
├── When to send
│   ├── Table: incident
│   ├── When: Record inserted or updated
│   └── Conditions: assigned_to changes AND is not empty
├── Who will receive
│   ├── Users: ${assigned_to}
│   └── Groups: (optional)
├── What it will contain
│   ├── Subject: Incident ${number} assigned to you
│   ├── Message: HTML body with ${field} references
│   └── Email Template: (optional)
└── Advanced
    ├── Weight: 0 (priority)
    └── Send to event creator: false
通知条件
// Simple field conditions
assigned_to CHANGES
priority = 1
state = 6  // Resolved

// Script condition (ES5 only!)
// Returns true to send, false to skip
(function() {
    // Only notify for VIP callers
    var caller = current.caller_id.getRefRecord();
    return caller.vip == true;
})()

// Advanced condition with multiple checks
(function() {
    // Don't notify on bulk updates
    if (current.sys_mod_count > 100) return false;

    // Only for production CIs
    var ci = current.cmdb_ci.getRefRecord();
    return ci.used_for == 'Production';
})()

メールテンプレート

テンプレート変数

${number}

${caller_id.name}

${opened_at.display_value}



${URI}

${URI_REF}

${mail_script:script_name}



${mailto:assigned_to}

テンプレートの例
<html>
  <body style="font-family: Arial, sans-serif;">
    <h2>Incident ${number} - ${short_description}</h2>

    <table border="0" cellpadding="5">
      <tr>
        <td><strong>Priority:</strong></td>
        <td>${priority}</td>
      </tr>
      <tr>
        <td><strong>Caller:</strong></td>
        <td>${caller_id.name}</td>
      </tr>
      <tr>
        <td><strong>Assigned to:</strong></td>
        <td>${assigned_to.name}</td>
      </tr>
      <tr>
        <td><strong>Description:</strong></td>
        <td>${description}</td>
      </tr>
    </table>

    <p>
      <a href="${URI}">View Incident</a>
    </p>

    ${mail_script:incident_history}
  </body>
</html>

メールスクリプト

基本的なメールスクリプト
// Email Script: incident_history
// Table: incident
// Script (ES5 only!):

;(function runMailScript(current, template, email, email_action, event) {
  // Build activity history
  var html = "<h3>Recent Activity</h3><ul>"

  var history = new GlideRecord("sys_journal_field")
  history.addQuery("element_id", current.sys_id)
  history.addQuery("name", "incident")
  history.orderByDesc("sys_created_on")
  history.setLimit(5)
  history.query()

  while (history.next()) {
    html += "<li><strong>" + history.sys_created_on.getDisplayValue() + "</strong>: "
    html += history.value.substring(0, 200) + "</li>"
  }
  html += "</ul>"

  template.print(html)
})(current, template, email, email_action, event)
添付ファイル付きメールスクリプト
// Add attachments from the record to the email
;(function runMailScript(current, template, email, email_action, event) {
  var gr = new GlideRecord("sys_attachment")
  gr.addQuery("table_sys_id", current.sys_id)
  gr.addQuery("table_name", "incident")
  gr.query()

  while (gr.next()) {
    email.addAttachment(gr)
  }
})(current, template, email, email_action, event)
動的な宛先
// Email Script to add CC recipients dynamically
;(function runMailScript(current, template, email, email_action, event) {
  // Add all group members as CC
  var group = current.assignment_group
  if (!group.nil()) {
    var members = new GlideRecord("sys_user_grmember")
    members.addQuery("group", group)
    members.query()

    while (members.next()) {
      var user = members.user.getRefRecord()
      if (user.email) {
        email.addAddress("cc", user.email, user.name)
      }
    }
  }
})(current, template, email, email_action, event)

カスタムイベント

カスタムイベントの登録
// Event Registration
// Name: x_myapp.incident.escalated
// Table: incident
// Description: Fired when incident is escalated to management
// Fired by: Business Rule

// In Business Rule (ES5 only!)
;(function executeRule(current, previous) {
  // Check if escalation occurred
  if (current.escalation > previous.escalation) {
    // Fire custom event
    gs.eventQueue(
      "x_myapp.incident.escalated",
      current,
      current.escalation.getDisplayValue(), // parm1
      current.assigned_to.name, // parm2
    )
  }
})(current, previous)
カスタムイベントの通知
Notification: Escalation Alert
├── When to send
│   ├── Send when: Event is fired
│   └── Event name: x_myapp.incident.escalated
├── Who will receive
│   └── Users/Groups: Escalation Managers
└── What it will contain
    ├── Subject: Escalation: ${number} - ${event.parm1}
    └── Message: Incident escalated. Assigned to: ${event.parm2}

宛先の種類

受信者
種類説明例
ユーザー特定のユーザー${assigned_to}、${caller_id}
グループユーザーグループサービスデスク、CAB
グループマネージャーグループのマネージャーフィールド${assignment_group.manager}
イベントパラメーター 1/2イベントパラメーターから${event.parm1}
追加の受信者メールアドレス外部メール
宛先スクリプト
// Recipient Script (ES5 only!)
// Returns comma-separated list of emails or sys_ids

;(function getRecipients(current, event) {
  var recipients = []

  // Add the caller
  if (!current.caller_id.nil()) {
    recipients.push(current.caller_id.email.toString())
  }

  // Add VIP's manager
  var caller = current.caller_id.getRefRecord()
  if (caller.vip == true && !caller.manager.nil()) {
    recipients.push(caller.manager.email.toString())
  }

  return recipients.join(",")
})(current, event)

通知の重み

複数の一致する通知に対する優先度システム:

重み用途
0デフォルトの優先度
1-99より高い優先度(重みが小さいほど優先度が高い)
-1 to -99より低い優先度
100+ほとんど使用されません
// Only highest weight notification sends if "Exclude subscribers" checked
// Weight 0 notification beats Weight 10 notification

ダイジェスト通知

ダイジェストの設定
Notification: Daily Incident Summary
├── Digest: Checked
├── Digest Interval: Daily
├── Digest Time: 08:00
└── Content: Summary of all incidents
ダイジェストメールスクリプト
// Summarize digest records
;(function runMailScript(current, template, email, email_action, event) {
  var count = 0
  var html = '<table border="1" cellpadding="5">'
  html += "<tr><th>Number</th><th>Description</th><th>Priority</th></tr>"

  // 'current' is a GlideRecord with all digest records
  while (current.next()) {
    count++
    html += "<tr>"
    html += "<td>" + current.number + "</td>"
    html += "<td>" + current.short_description + "</td>"
    html += "<td>" + current.priority.getDisplayValue() + "</td>"
    html += "</tr>"
  }
  html += "</table>"
  html += "<p>Total: " + count + " incidents</p>"

  template.print(html)
})(current, template, email, email_action, event)

送信メールの設定

メールプロパティ
// System Properties for email
glide.email.smtp.active // Enable/disable outbound email
glide.email.smtp.host // SMTP server
glide.email.smtp.port // SMTP port (usually 25 or 587)
glide.email.default.sender // Default from address
glide.email.test.user // Test recipient (all emails go here)
通知のテスト
// Background Script to test notification (ES5 only!)
var gr = new GlideRecord("incident")
gr.get("sys_id_here")

// Fire event to trigger notification
gs.eventQueue("incident.assigned", gr, gr.assigned_to.getDisplayValue(), gs.getUserDisplayName())

gs.info("Event queued for incident: " + gr.number)

ベストプラクティス

  1. テンプレートを使用する - 通知間でレイアウトを再利用します
  2. 十分にテストする - 開発中はテストユーザープロパティを使用します
  3. ダイジェストを検討する - 通知量が多い場合に使用します
  4. 重みを慎重に設定する - メールの重複を防ぎます
  5. ES5 のみ - すべてのスクリプトは ES5 に準拠する必要があります
  6. 宛先を制限する - 大人数のグループにスパムを送信しないでください
  7. コンテキストを含める - ログインしなくても対応できる十分な情報を提供します
  8. モバイルに配慮する - モバイルクライアント向けに HTML をシンプルに保ちます

よくある問題

問題原因解決策
メールが送信されないイベントが発火していないビジネスルールでイベントが発火するか確認します
宛先が間違っているスクリプトエラー宛先スクリプトをデバッグします
コンテンツが欠落しているテンプレート変数が間違っているフィールド名を確認します
メールが重複する複数の通知重みと条件を確認します
メールが遅延するメールジョブのスケジュールsysauto_script を確認します

コメント