メール通知のスクリプトの例

  • リリースバージョン: Zurich
  • 更新日 2025年07月31日
  • 所要時間:3分
  • メール通知のスクリプトの例

    メール通知のスクリプトの例

    シンプル テキスト文字列は、メール スクリプトが機能する最も基本的な例です。このスクリプトでは、"Incident number - INC00001" のように表示されます。
    template.print("Incident number - "+ current.number);
    次のような高度なスクリプトは、基本システムのメール テンプレートから見つけることができます。
    template.print("Summary of Requested items:<br />");  
    var now_GR = new GlideRecord("sc_req_item");
    now_GR.addQuery("request", current.sysapproval);
    now_GR.query();
    while(now_GR.next()) {
      template.print(now_GR.number + ":  " + now_GR.quantity + " X " + now_GR.cat_item.getDisplayValue() 
                       + " at " + now_GR.cat_item.price.getDisplayValue() + " each <br />");
    }
    メール内のフィールド値を動的に変更するには、<mail_script> 構文内で次の関数を使用します。
    ...
    email.setFrom(current.caller_id.email);
    email.setReplyTo("joe.employee@yourcompany.com");
    email.setSubject("This is the new subject line");
    email.setBody("This is the new body");
    ...
    instance_name プロパティを使用することで、インスタンス間で移行されたときに確実に通知をそのまま機能させることができます。
    dothis();
     
    function dothis(){
        var now_GR =new GlideRecord('sys_attachment');
        now_GR.addQuery('table_sys_id',current.sys_id);
        now_GR.query();while(now_GR.next()){
            template.print('Attachment: <a href="https://'+ gs.getProperty('instance_name')+'             .service-now.com/sys_attachment.do?sys_id='+ now_GR.sys_id+'">'+ now_GR.file_name+'</a>');}}
    メール スクリプト内のメール オブジェクトを使用して、cc の受信者と bcc の受信者を指定することができます。
    //email.addAddress(type, address, displayname);
        email.addAddress("cc","john.copy@example.com","John Roberts");
        email.addAddress("bcc","john.secret@example.com","John Roberts");
    以下は、ユーザーを watch_list から cc の受信者として追加するスクリプトの例です。
    if(!current.watch_list.nil()){
       //get watch list addresses and add to cc
       var watcherIds = current.watch_list.split(",");
     
       //get user records
       var user = new GlideRecord("sys_user");
       user.addQuery("sys_id", watcherIds);
       user.addQuery("notification",2); 
       //email
       user.addQuery("email","!=","");
       user.query();
     
       while(user.next()){
          //add to cc list    
          email.addAddress("cc", user.email, user.getDisplayValue());}}