Set Up a Formula to Sync Salesforce and Shopify Contacts

You can build formula templates — reusable workflow templates that are independent of API providers. Afterwards, you create a formula instance where you plug your values or specific elements instances into the workflow (in this case, your Salesforce and Shopify).  Formula templates include triggers, like events or schedules, that kick off a series of steps. Formulas support a large variety of different use cases across different services. For example, they can keep systems in sync, migrate data between systems, or automate business workflows.

For this tutorial, you'll create a formula template that:

  1. Kicks off when you update a contact.
  2. Checks to see if the contact is new or if it was updated (which it will ignore).
  3. Gets the contact from your Salesforce Sales Cloud element instance and posts it to your Shopify instance.

Before we start, it's important to understand the difference between a formula template and a formula instance. The template defines how the formula works regardless of variables like the kind of element or a user input. With formula instances you plug actual values into the variables in the template. With this approach you can plug different element instances into the formula so you can sync contacts with Hubspot, Woocommerce, Zoho, Etsy, Magento, or any others.

If you want to see a completed formula template for comparison, download our sample copy, and then import it.

Step 1. Set Up the Formula Template Triggers and Variables

The trigger used in a formula template is the action that kicks off the formula. You can set up triggers for events, scheduled jobs, API requests to elements, and manual triggering. Variables can represent authenticated element instances or values. When you actually run a formula by creating a formula instance, you replace the variables with actual authenticated element instances or values.

For this tutorial, the trigger will be based on an event — when a new contact is created in Salesforce. The variables will represent the Salesforce Sales Cloud and Shopify element instances that you created earlier.

To set up triggers and variables:

  1. Navigate to the Formulas page. From anywhere in the application, click Formulas on the left side.
  2. On the Formulas page, click Build New Formula Template.

    You can build formula templates based on existing templates or by importing a JSON file that defines the template. For this tutorial, you will build the formula template from scratch. However, if you want to test your formula with ours, download our copy, and then import it later.

  3. Click Build New Formula.
  4. Enter a name for your formula like "Sync Contacts", and then click Create.

    Choose the trigger. Triggers define how the formula starts. Because we want to automatically sync contacts when our event framework discovers a new /Contact, we'll use an Event trigger. When new or updated contacts appear in Salesforce, our Event framework builds a payload that can trigger a Formula.

    New Formula Template

  5. Click Event.

    As mentioned earlier, formula templates consist of variables. The variables can represent authenticated element instances or values. In this formula, the variables will represent the two element instances that you authenticated earlier. Because you have not yet created a variable you need to create one to represent the element that kicks off the formula.

  6. Click the add button Add.
  7. On the Formula Variables window, click Add New Variable, and then click Element Instance.
  8. Name the variable that represents the element that kicks off the template OriginInstance. In our example, this will be Salesforce Sales Cloud.

    The variable should look like the example below:

    Origin Instance

  9. Click Save.
  10. Select the variable that you just created OriginInstance, and then on the Edit event: "trigger" window click Save.
  11. Now add a variable for the element that gets updated when a contact is created (Shopify): Click Variables in the menu bar.



  12. Click Element Instance.

  13. Name the variable DestinationInstance, and then click Save.

At this point you built a template based on an Event trigger with two variables to represent the origin and destination elements. Next, add the steps needed to perform the sync.

Step 2: Add Steps to the Formula Template

In addition to triggers and variables, formula template include steps that represent actions that the formula needs to take to accomplish its goal. At the end of every step, the formula can follow an On Success or On Failure path. For this tutorial, all steps will follow the On Success path. The steps we'll add include:

  • Create a Filter.
  • Retrieve the contact.
  • Create a new contact.

To add steps:

  1. Under the trigger click .

    You can use many different kinds of steps in a formula. Because we're going to write some logic to make sure that the event we receive is for a created contact, we'll use the JS Script step type.

  2. Click JS Script.
  3. Enter a name for the step like "IsCreateContact".
  4. In the code area, enter the following, and then click Save.

    let theEvent = trigger.event.eventType;
    let theObject = trigger.event.objectType;
    
    done((theEvent === 'CREATED') && (theObject === 'Contact' || theObject === 'contact'));
    
  5. Now add the next step to retrieve the created contact from the element represented by the OriginInstance variable — Salesforce Sales Cloud. Click the IsCreateContact step that you just created, and then click Add OnSuccess.

    Add On Success

  6. This time select the Element API Request step type. With this type of step, you need to identify the element variable to request and what the endpoint should be.
  7. In the Add Formula Step window, enter the name for the step: "RetrieveContact".
  8. In Element Instance Variable click Add, and then select OriginInstance.
  9. In Method, select GET.
  10. In API enter /hubs/crm/myContact/${trigger.event.objectId}. This is the endpoint of the virtual data resource that you created earlier.
    Note: ${trigger.event.objectId} refers to the context of the formula. When a trigger receives the event payload, it includes data that you can manipulate. In the case, we are creating a request to get the payload returned by an event with a specific objectId.
    Also see that the API request endpoint is the virtual data resource that we created earlier, so only the data we care about will be posted to Shopify.
  11. Click Save.

  12. Now create the final step, which is posting the created contact to the element represented by the destination variable — Shopify. Create another Element API Request with the information below:

FieldValue
NameCreateContact
Element Instanceconfig.destinationinstance
MethodPOST
API/hubs/ecommerce/myContact
Bodysteps.RetrieveContact.response.body

Note: Click Show Advanced to find the Body field. This also uses the context of the formula to look in the response to the RetrieveContact step to compose the body of its own request.

Step 3. Create a Formula Instance

You are almost done. Now we just need to create and run a formula instance to see the actual sync occur.

The formula instance actually performs the sync. In this case we're going to associate the Salesforce Sales Cloud element instance with the OriginInstance formula variable, and Shopify with DestinationInstance. Whenever you create a new contact in Salesforce, the poller recognizes that an even occurred. The trigger for the formula kicks off when an event occurs at the element in the OriginInstance variable, which then kicks off the logic in the formula.

To create a formula instance:

  1. Navigate back to the main Formulas page.
  2. Hover over the Sync Contacts card, and then click Create Instance.
  3. Name the instance something more specific than the template like "Salesforce to Shopify Contact Sync."
  4. ClickOriginInstance, and then select the Salesforce instance that you authenticated earlier — Salesforce Contacts.
  5. ClickDestinationInstance, and then select the Shopify instance that you authenticated earlier — Shopify Customers.

    You're formula instance should look like the example below.
    Formula Instance - Sync Contacts

  6. Click Create Instance.

Step 4. Try it Out

  1. Log in in your Salesforce account and create a new contact.
  2. Wait a few minutes — remember that we set the polling interval for five minutes — and return to Cloud Elements 2.0.
  3. On the Formulas page, hover over the Sync Contacts card, and then click Executions.
  4. The executions appear on the left side, listed by Object ID. You can click the Execution itself to see the series of steps, and you can get even more detailed by clicking on each step.
  5. Lastly, log in to Shopify to see the new Customer that has been created in your account.