SendGrid: 宛先, 追加・更新 (SendGrid: Recipient, Update)
SendGrid コンタクトに宛先を追加します。既に同じメールアドレスの宛先が登録されている場合は、予約フィールド・カスタムフィールドの値が更新されます。
Configs
  • C1: API トークンを設定した認証設定 *
  • C2: メールアドレス *#{EL}
  • C3: First Name#{EL}
  • C4: Last Name#{EL}
  • C5C: カスタムフィールド名_1
  • C5V: 値_1#{EL}
  • C6C: カスタムフィールド名_2
  • C6V: 値_2#{EL}
  • C7C: カスタムフィールド名_3
  • C7V: 値_3#{EL}
  • C8C: カスタムフィールド名_4
  • C8V: 値_4#{EL}
  • C9C: カスタムフィールド名_5
  • C9V: 値_5#{EL}
  • C10C: カスタムフィールド名_6
  • C10V: 値_6#{EL}
  • C11C: カスタムフィールド名_7
  • C11V: 値_7#{EL}
  • C12C: カスタムフィールド名_8
  • C12V: 値_8#{EL}
Script (click to open)


addRecipients(createRecipientsObj());

function createRecipientsObj(){

    const CUSTOM_FIELD_NUM = 8; // 扱えるカスタムフィールドの数

    const firstName = configs.get("conf_firstName");
    const lastName = configs.get("conf_lastName");

    const recipientsObj = [];
    const recipient = {};
    recipient["email"] = configs.get("conf_email");
    if( firstName !== null){
        recipient["first_name"] = firstName;
    }
    if( lastName !== null){
        recipient["last_name"] = lastName;
    }
   
    for (let i = 0; i < CUSTOM_FIELD_NUM; i++) {

        const customFieldNameConfigName = `conf_customFieldName${i+1}`;
        const customFieldValueConfigName = `conf_customFieldValue${i+1}`;
        const customFieldName = configs.get( customFieldNameConfigName );

        if ( customFieldName === null || customFieldName === "" ) { // カスタムフィールド名が空
            continue;
        }

        if ( recipient[customFieldName] !== undefined ) { // カスタムフィールド名の指定が重複
            throw "The same Field Code is set multiple times.";
        }

        let customFieldValue = configs.get( customFieldValueConfigName );
        if ( customFieldValue !== null ) { // 値が空でない場合に値を追加
            recipient[customFieldName] = customFieldValue;
        }

    }

    recipientsObj[0] = recipient;
    engine.log(JSON.stringify(recipientsObj));

    return recipientsObj;

}

function addRecipients(recipientsObj){

    const auth = configs.get("conf_auth");
    const url = "https://api.sendgrid.com/v3/contactdb/recipients";

    const response = httpClient.begin()
    .authSetting(auth)
    .body(JSON.stringify(recipientsObj),"application/json; charset=UTF-8")
    .post(url);

    engine.log("HTTP Status: " + String(response.getStatusCode()));

    const responseJson = JSON.parse(response.getResponseAsString());
    if(responseJson.error_count > 0){
        let errorMessage = "";
        for(let i = 0 ; i < responseJson.errors.length ; i++){
            errorMessage = responseJson.errors[i].message + "\n";
        }
        throw errorMessage;
    }

}


Download

2021-09-09 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/ja/addons/sendgrid-recipient-update/
Addonファイルのインポートは Professional でご利用いただけます

Notes

  • 本アドオンを利用するワークフローアプリの HTTP 認証設定では、SendGrid における Settings > API Keys で作った API Key を追加します。
  • 「C2: メールアドレス」で指定したメールアドレスが、SendGrid コンタクト(Contacts)に登録されている場合は、追加ではなく更新(上書き)されます。
  • カスタムフィールド名には、SendGrid コンタクト(Contacts)における Custom Fields の NAME を入力してください。

Capture

%d