# Salesforce, JavaScript snippets by Workato, and Snowflake recipe

This use case guides you through connecting Salesforce, JavaScript snippets by Workato, Snowflake, and Email by Workato to create a powerful automation.

# What does this recipe do?

This recipe retrieves new Salesforce contacts in batches, validates each phone number using JavaScript, and upserts the valid contacts into a Snowflake table. Contacts with invalid phone numbers are excluded from the upsert, and the recipe sends an email listing these invalid contacts.

flowchart TD subgraph Configuration[" "] direction TB subgraph Salesforce[&nbsp Salesforce New records batch trigger &nbsp] t1(Trigger) --- setup1(setup) end subgraph JavaScript[&nbsp JavaScript snippets by Workato Execute code action &nbsp] Action1(Action) --- setup2(setup) end subgraph Snowflake[&nbsp Snowflake Replicate rows batch action &nbsp] Action2(Action) --- setup3(setup) end subgraph Email[&nbsp Email by Workato Send email action &nbsp] Action3(Action) --- setup5(setup) end end CreateRecipe([Create recipe]) -- Configure your recipe --> Configuration --> Workflow([Automated workflow]) Salesforce --> JavaScript --> Snowflake --> Email classDef default fill:#fff,stroke:#67eadd,stroke-width:2px; classDef WorkatoTeal fill:#67eadd,stroke:#b3e0e1,stroke-width:2px,color:#000; classDef SubgraphDash fill:#e1fffc,stroke:#f66,stroke-width:2px,color:#000,stroke-dasharray: 5 5 class t1,setup1,setup2,setup3,setup4,setup5,Action1,Action2,Action3,x,xx,CreateRecipe,Workflow WorkatoTeal class Salesforce,JavaScript,Snowflake,Email SubgraphDash

# Create your recipe

Complete the following steps to create a recipe that retrieves new Salesforce contacts in batches, validates each phone number using JavaScript, and inserts or updates rows in a Snowflake table:

USE CASES ARE INTENDED AS EXAMPLES ONLY

This use case is intended to serve as an example. Recipe modifications such as trigger and action configuration and conditional logic may require adjustments if you plan to include this recipe in your own workflow.

1

Sign in to your Workato account and go to the workspace project where you plan to add the recipe.

2

Set up connections for Salesforce and Snowflake using the steps in the following sections:

WORKATO UTILITY CONNECTORS DON'T REQUIRE SETUP

The JavaScript snippets by Workato and Email by Workato connectors do not require connection setup.

Set up your Salesforce connection.
1

Click Create > Connection.

2

Search for and select Salesforce in the New connection page.

3

Provide a name for your connection in the Connection name field.

Salesforce connection setupSalesforce connection setup

4

Use the Location drop-down menu to select the project where you plan to store the connection.

5

Use the Auth type drop-down menu to select the authentication method. The default is OAuth 2.0.

6

Use the Sandbox drop-down menu to specify whether the Salesforce account is a sandbox account.

7

Optional. Expand Advanced settings to configure advanced connection options.

8

Optional. Use the Custom OAuth profile drop-down menu to select a custom OAuth profile for your connection.

  • This custom OAuth profile ensures that the connection is restricted to the same set of scopes you selected for all users with the profile, and the authentication flow uses the client app linked to the custom profile.
9

Click Connect.

10

Enter your Salesforce account credentials and click Log In.

Salesforce connection setupLog in to your Salesforce account

Set up your Snowflake connection.
1

Click Create > Connection.

2

Search for and select Snowflake in the New connection page.

3

Provide a name for your connection in the Connection name field.

Snowflake connection setupSnowflake connection setup

4

Use the Location drop-down menu to select the project where you plan to store the connection.

5

Enter your account identifier in the Account identifier field. Snowflake has multiple methods of identifying an account. Workato supports all methods:

  • Account name: https://{org.name}-{account_name}
  • Connection name: https://{org.name}-{connectionname}
  • Account locator: https://{account.locator}.{region}.{cloud}

If you're using the account locator, note that {region} and {cloud} are only required for certain locations. For example:

  • If your account is hosted in AWS US West (Oregon), use your-account-locator.
  • If your account is hosted in AWS US East (Ohio), use your-account-locator.us-east-2.
  • If your account is hosted in Azure West Europe, use your-account-locator.west-europe.azure.
6

Enter the full name of the warehouse where you plan to perform all operations for this connection in the Warehouse field. For example, compute_wh.

7

Enter the database you plan to use in your recipe connection in the Database name field.

8

Use the Authentication type drop-down menu to select your authentication type.

  • Username/Password: Enter your username and password if you select this option.
  • OAuth 2.0: Enter your Client ID and Client Secret if you select this option.
9

Click Connect.

3

Click Create > Recipe.

4

Provide a name for your recipe in the Name field and select the project in which you plan to store the recipe from the Location drop-down menu.

5

Ensure that Trigger from an app is selected under Pick a starting point and click Start building.

Set up your recipeSet up your recipe

6
Set up your Salesforce New records batch trigger.
1

Search for and select Salesforce in the Choose an app search box.

Choose SalesforceChoose Salesforce

2

Select New records as your trigger.

Select the New records batch triggerSelect the New records batch trigger

3

Select the Salesforce connection you created in the preceding steps.

4

Use the Object drop-down menu to select Contact as the Salesforce object you plan to monitor for new records.

5

Optional. Use the Fields to retrieve drop-down menu to specify the fields you plan to retrieve. This example selects the following fields:

  • Contact ID
  • Account ID
  • Last Name
  • First Name
  • Business Phone
  • Email

Specify the fields you plan to retrieveSpecify the fields you plan to retrieve

6

Enter the batch size in the Batch size field. The default is 100, and the maximum is 2000 records per batch.

7

Optional. Click Show optional fields, select Trigger poll interval, and click Apply changes.

Configure the Trigger poll intervalConfigure the Trigger poll interval

8

Optional. Use the Trigger poll interval drop-down menu to specify how frequently this recipe should check for new contacts in Salesforce. This example selects 1 day.

9

Click Save.

Step summary

This trigger monitors Salesforce daily for new contact records and retrieves specific fields when new contacts are found.

7

Click the + Add step button and select Action in app.

Add actionClick Add step > Add action in app

8
Set up your JavaScript snippets by Workato Execute code action.
1

Search for and select JavaScript snippets by Workato in the Choose an app search box.

Choose JavaScript snippets by WorkatoChoose JavaScript snippets by Workato

2

Select Execute code as your action.

Select the Execute code actionSelect the Execute code action

3

Enter Validate contacts in the Name field. This changes the action name in the recipe editor.

Configure the Name fieldConfigure the Name field

4

Go to Input fields and select Add field.

1

Enter contacts in the Name field.

2

Click Add field.

Select the Execute code actionSelect the Execute code action

5

Switch the Contacts field to formula mode.

6

Map the Salesforce Contacts | Step 1 list datapill to the Contacts field. You can't map this datapill unless the Contacts field is switched to formula mode.

Map the Contacts datapillMap the Contacts datapill

7

Go to Output fields and create a placeholder field.

8

Click Edit schema.

9

Copy and paste the following JSON schema to replace the existing schema:

[
  {
    "name": "val_contacts",
    "type": "array",
    "of": "object",
    "label": "Val contacts",
    "properties": [
      {
        "properties": [
          {
            "control_type": "text",
            "label": "Type",
            "type": "string",
            "name": "type"
          },
          {
            "control_type": "text",
            "label": "URL",
            "type": "string",
            "name": "url"
          }
        ],
        "label": "Attributes",
        "type": "object",
        "name": "attributes"
      },
      {
        "control_type": "text",
        "label": "Account ID",
        "type": "string",
        "name": "AccountId"
      },
      {
        "control_type": "text",
        "label": "Phone",
        "type": "string",
        "name": "Phone"
      },
      {
        "control_type": "text",
        "label": "ID",
        "type": "string",
        "name": "Id"
      },
      {
        "control_type": "text",
        "label": "Email",
        "type": "string",
        "name": "Email"
      },
      {
        "control_type": "text",
        "label": "First name",
        "type": "string",
        "name": "FirstName"
      },
      {
        "control_type": "text",
        "label": "Last name",
        "type": "string",
        "name": "LastName"
      },
      {
        "control_type": "text",
        "label": "Valid",
        "render_input": {},
        "parse_output": {},
        "toggle_hint": "Select from option list",
        "toggle_field": {
          "label": "Valid",
          "control_type": "text",
          "toggle_hint": "Use custom value",
          "type": "boolean",
          "name": "valid"
        },
        "type": "boolean",
        "name": "valid"
      }
    ]
  },
  {
    "name": "bad_contacts",
    "type": "array",
    "of": "object",
    "label": "Bad contacts",
    "properties": [
      {
        "properties": [
          {
            "control_type": "text",
            "label": "Type",
            "type": "string",
            "name": "type"
          },
          {
            "control_type": "text",
            "label": "URL",
            "type": "string",
            "name": "url"
          }
        ],
        "label": "Attributes",
        "type": "object",
        "name": "attributes"
      },
      {
        "control_type": "text",
        "label": "Account ID",
        "type": "string",
        "name": "AccountId"
      },
      {
        "control_type": "text",
        "label": "Phone",
        "type": "string",
        "name": "Phone"
      },
      {
        "control_type": "text",
        "label": "ID",
        "type": "string",
        "name": "Id"
      },
      {
        "control_type": "text",
        "label": "Email",
        "type": "string",
        "name": "Email"
      },
      {
        "control_type": "text",
        "label": "First name",
        "type": "string",
        "name": "FirstName"
      },
      {
        "control_type": "text",
        "label": "Last name",
        "type": "string",
        "name": "LastName"
      },
      {
        "control_type": "text",
        "label": "Valid",
        "render_input": {},
        "parse_output": {},
        "toggle_hint": "Select from option list",
        "toggle_field": {
          "label": "Valid",
          "control_type": "text",
          "toggle_hint": "Use custom value",
          "type": "boolean",
          "name": "valid"
        },
        "type": "boolean",
        "name": "valid"
      }
    ]
  },
  {
    "control_type": "text",
    "label": "Log",
    "type": "string",
    "name": "log"
  }
]
10

Click Update to apply the new JSON schema. You can scroll through the listings under Output fields to see the newly applied schema output:

Output fieldsOutput fields

You can modify this schema based on the fields you plan to retrieve from Salesforce and upsert into Snowflake.

11

Clear the contents of the Code field.

12

Copy and paste the following JavaScript code into the Code field:

const _ = require("lodash");

class ContactValidator {
  static validContact = (phone) => {
    return this.validPhoneNumber(phone);
  };

  static validPhoneNumber = (phone) => {
    let phone_regex = /^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/;
    return phone ? phone_regex.test(phone) : false;
  };

  static validate = function(contacts) {
    const bad_contacts = [];
    const val_contacts = _.filter(contacts, (contact) => {
      let phone = _.get(contact, 'Phone');
      const isValid = this.validContact(phone);
      contact['valid'] = isValid;

      if (!isValid) {
        bad_contacts.push(contact); // Add invalid contact to the array
        return false;  // Don't include invalid contacts in val_contacts
      }
      return true;  // Include only valid contacts in val_contacts
    });

    // Create a log message based on the bad contacts
    const logMessage = bad_contacts.length > 0
      ? `The following contacts were not replicated due to invalid phone numbers: ${bad_contacts.map(c => `${c.FirstName} ${c.LastName} (${c.Phone})`).join(', ')}`
      : 'All contacts were valid and successfully replicated.';

    return { val_contacts, bad_contacts, log: logMessage };
  };
}

exports.main = ({ contacts }) => {
  return ContactValidator.validate(contacts);
};
13

Click Save.

Step summary

This step validates contacts by checking if each record contains a valid phone number. It returns the following output fields: val_contacts (list of valid contacts), bad_contacts (list of invalid contacts), and log. The log message varies based on whether all Salesforce contacts in the batch are valid or if any have invalid phone numbers.

9

Click the + Add step button and select Action in app.

10
Set up your Snowflake Replicate rows batch action.
1

Search for and select Snowflake in the Choose an app search box.

Choose SnowflakeChoose Snowflake

2

Select Replicate rows as your action.

Select the Replicate rows batch actionSelect the Replicate rows batch action

3

Select the Snowflake connection you created in the preceding steps.

4

Use the Table drop-down menu to select the Snowflake table where you plan to replicate rows.

5

Ensure that the Rows field is switched to formula mode and map the JavaScript snippets by Workato Val contacts | Step 2 datapill.

Map the Val contacts datapillMap the Val contacts datapill

6

Use the Flatten columns drop-down menu to select Yes.

7

Enter 1 in the Flatten level field.

8

Use the Unique keys drop-down menu to select one or more unique keys to identify duplicate rows. This example selects ID (contact ID) as the unique key.

Select the unique keySelect the unique key

9

Optional. Use the Exclude columns drop-down menu to select the columns to exclude while replicating rows. This example selects Attributes and Valid.

Select the columns to exclude from the upsertSelect the columns to exclude from the upsert

10

Click Save.

Step summary

This step inserts or updates the records from the valid contacts list into the Snowflake table.

11

Click the + Add step button and select IF condition.

Add IF conditionClick Add step > IF condition

How do IF conditions work?

An IF condition enables you to make decisions based on specific conditions. You can control your recipe workflow by executing different actions depending on whether a condition you specify is true or false.

flowchart TD A([Replicate valid Salesforce \n contact rows to Snowflake]) --> B(Were there any \n invalid contacts?) B -->|Yes| C(Send an email detailing the \n contacts with invalid phone numbers) B ---->|No| E(No action required) classDef default fill:#67eadd,stroke:#b3e0e1,stroke-width:2px,color:#000; classDef WorkatoPink fill:#f3c1c2,stroke:#f3c1c2,stroke-width:1px; class E WorkatoPink
12
Set up your IF condition.
1

Switch Data field to formula mode.

2

Map the JavaScript snippets by Workato Bad contacts | Step 2 datapill to Data field.

3

Append the .length formula.

4

Use the Condition drop-down menu to select greater than.

5

Switch the Value field to formula mode and enter 0.

Configure the IF conditionConfigure the IF condition

6

Click Save.

Step summary

This step checks if the bad contacts list size is greater than zero. If this condition is true, this means that the batch contains one or more contacts with invalid phone numbers.

13

Click Select an app and action.

14
Set up your Email by Workato Send email action.
1

Search for and select Email by Workato in the Choose an app search box.

Choose Email by WorkatoChoose Email by Workato

2

Select Send email as your action.

Select the Send email actionSelect the Send email action

3

Provide the recipient's email address in the To field.

4

Enter a subject line in the Subject field. For example, Salesforce Contacts Not Replicated into Snowflake Due to Invalid Data

5

Use the Email type drop-down menu to select Text.

6

Map the JavaScript snippets by Workato Log | Step 2 datapill to the Message field.

Configure the messageConfigure the message

7

Click Save.

Step summary

If the bad contacts list size is greater than zero, Workato sends an email to a recipient you specify detailing which Salesforce contacts were not replicated into Snowflake due to invalid phone numbers.

Your Salesforce, JavaScript snippets by Workato, Snowflake, and Email by Workato recipe is ready to test and implement.
Example recipe configuration.

Salesforce, JavaScript snippets by Workato, Snowflake, and Email by Workato recipeSalesforce, JavaScript snippets by Workato, Snowflake, and Email by Workato recipe


Last updated: 11/4/2024, 6:09:46 PM