SendGrid: Recipient, Add or Update

SendGrid: Recipient, Add or Update
Adds a destination to the SendGrid contact. If a destination with the same email address has already been registered, the value of the reserved and custom fields will be updated.
Configs
  • C1: Authorization Setting in which API Token is set *
  • C2: email *#{EL}
  • C3: First Name#{EL}
  • C4: Last Name#{EL}
  • C5C: Custom Field Name 1
  • C5V: Value 1#{EL}
  • C6C: Custom Field Name 2
  • C6V: Value 2#{EL}
  • C7C: Custom Field Name 3
  • C7V: Value 3#{EL}
  • C8C: Custom Field Name 4
  • C8V: Value 4#{EL}
  • C9C: Custom Field Name 5
  • C9V: Value 5#{EL}
  • C10C: Custom Field Name 6
  • C10V: Value 6#{EL}
  • C11C: Custom Field Name 7
  • C11V: Value 7#{EL}
  • C12C: Custom Field Name 8
  • C12V: Value 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/addons/sendgrid-recipient-update/
The Add-on import feature is available with Professional edition.

Notes

  • In the HTTP Authorization Settings of the workflow App in which this Add-on is to be used, add the API Key that you created in SendGrid under Settings > API Keys.
  • If the email address specified in “C2: Email Address” is already registered in SendGrid Contacts, it will be updated instead of added.
  • In the Custom Field Name field, enter the name of the Custom Field in the SendGrid Contact.

Capture

Scroll to Top

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading