---
sourceDocument: Xanadu Now Platform の管理
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/ja-JP/xanadu/platform-administration

 Release :

    - xanadu

ft:locale :

    - ja-JP

ft:publication_title :

    - Xanadu Now Platform の管理

ft:clusterId :

    - platadm

bundleId :

    - platadm

workflow :

    - Platform


---

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

# メール通知のスクリプトの例 {#ariaid-title1}

* リリースバージョン: Xanadu
* 
* 更新日 2024年08月01日
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 所要時間：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());}}

**関連概念**   

* [メールの JavaScript](https://servicenow-prod.fluidtopics.net/TB9FuF6Tu_zV2M3L_sRgrQ "でのメールスクリプトの作成 システム通知 > メール > 通知メールスクリプト をクリックし、スクリプトフィールドで ${mail_script:script name} を使用して参照します。")  
**関連資料**   

* [メール スクリプト API](https://servicenow-prod.fluidtopics.net/1fyhVjrPoeV5d_kGDLXozQ "mail_script スクリプトを処理する際は、特定の変数を使用できます。")
* [便利な添付ファイル スクリプト](https://servicenow-prod.fluidtopics.net/LAtFLfqqNDhgOdZz59LmMQ "これは、便利な添付ファイル スクリプトの検索可能なバージョンです。")  
**関連情報**   

* [TemplatePrinter API](https://www.servicenow.com/docs/access?context=c_TemplatePrinterScopedAPI&version=xanadu&pubname=xanadu-api-reference&ft:locale=en-US)
* [GlideEmailOutbound API](https://www.servicenow.com/docs/access?context=c_GlideEmailOutboundScopedAPI&version=xanadu&pubname=xanadu-api-reference&ft:locale=en-US)

