Create a Lead in Salesforce

Website: https://www.salesforce.com/ API Docs: https://developer.salesforce.com/page/Salesforce_APIs

Salesforce is a customer relationship management (CRM) solution. In our example, we're going to show you how to configure Salesforce to send leads from Customer.io into Salesforce.

Show me how!

The final product:

Create a Lead in Salesforce with Customer.io

In this walkthrough, we'll first show you how to configure Salesforce to enables its API and connect Customer.io as a "Connected App". Then, we'll show you how to create a lead in Salesforce with Customer.io.

Get Started Configuring Salesforce

Let's make sure API access is enabled in Salesforce first. The easiest way to test this is by opening the “Developer Console” from the “Setup” menu and to open a resource.

Ensure API Access is Enabled

This will open the Developer Console in a new window. Navigate to “Open Resource” in the “File” menu.

If API access is enabled, you should see another window with the available resources. If API access is disabled, you'll either see a message like "The REST API is not enabled for this Organization." or "API_DISABLED_FOR_ORG".

If your Salesforce API access is disabled, you'll have to contact your Account Manager and have them enable it for you.

Set Up a Connected App

Navigate to the “Setup Home”. Go to the “Apps” under “Platform Tools” and create a new “Connected App”.

For the Connected App check the “Enable OAuth Settings” checkbox and select “Full Access” scope.

Once created, you'll have access to the “Consumer Key” and “Consumer Secret” which will be used in the final request to get the access token.

Configure OAuth IP Restrictions

By default, IP restrictions are enforced—so you can either add “Trusted IP Range for OAuth Web server flow” or change the settings to disable IP restrictions.

In order to disable the IP restrictions, click the “Manage” button for the app and then click the “Edit” button. Next, set the IP relaxation setting to “Relax IP restrictions”.

Reset Security Token

The final piece of information needed is your security token. Navigate to your personal settings, then to “Reset My Security Token”, and click the “Reset Security Token” button. Your token will be emailed to you.

Retrieve Access Token

Now you can generate access tokens to authorize web requests to update Salesforce.

Replace the [variables] in the curl request below and make the request:

curl -XPOST https://login.salesforce.com/services/oauth2/token \
-d "grant_type=password&client_id=[ConsumerKey]&client_secret=[ConsumerSecret]
&username=[LoginEmail]&password=[LoginPassword][SecurityToken]" \
-H "X-PrettyPrint: 1"

This will return a response like:

{
"access_token" : "00D50000000NGi2!AQQAQH9ob.5uh.QjYSmJ71mTxya8ziK.Y_Z7q8kNsTFJoF0jJEJEBLKo_zBNKMFbHx253rzrjk84WGHl4.YdSaxLsIqO2iA9",
"instance_url" : "https://na3.salesforce.com",
"id" : "https://login.salesforce.com/id/00D50000000NGi2EAG/00550000005pnXCAAY",
"token_type" : "Bearer",
"issued_at" : "1469540453130",
"signature" : "9vlwhHvRx+8N2QVbpDxsneQGGW7mNkYU+FGjLPB7ykQ="
}

We will be using the instance_url and the access_token for our web requests.

Create a Generate Lead in Salesforce Campaign

You can use either an event triggered or a segment triggered campaign depending on which best suits your use case. For this tutorial, we'll use a segment triggered campaign with the default “Signed Up” segment.

Add A Webhook Action

On the workflow page select “Webhook” from the “Add Action” dropdown. We'll call our webhook "Create Salesforce Lead"

Create Your Webhook URL

Next, click "Add Request" button on the webhook to configure its details.

In the composer view, we'll set up the URL, authorization headers, and the request body to be able to generate leads in Salesforce. In this step, we'll use the instance_url and the access_token from the Salesforce call we made earlier.

The url for the request will be:

[instance_url]/services/data/v37.0/sobjects/Lead/Email/{{customer.email}}?_HttpMethod=PATCH

A few points to note about the URL:

  • we use the instance_url from the Salesforce call as the base of our URL .
  • The {{customer.email}} is a Liquid tag, which automatically gets replaced with the email for the new customers flowing through the campaign.
  • We're sending the query param _HttpMethod=PATCH so that the we will either create a new lead with the email provided or update the existing lead with the data provided.

So for our example, the URL looks like this:

https://na3.salesforce.com/services/data/v37.0/sobjects/Lead/Email/{{customer.email}}?_HttpMethod=PATCH
Add Authorization Header

Next, click “Add Header” and provide your access_token. The name of the header should be “Authorization” and the Value must be “Bearer [access_token]”.

Compose your webhook's body

In order to create a lead in Salesforce, you’ll likely want to send details like name, email address, and custom data. For our example, we'll send three attributes to Salesforce: first name, last name, and company name.

(Since we're passing the email address in the URL, we don't need to specify it in the body. Salesforce will automatically associate the email with the record.)

Send Test Request

If you have customers in your account that match the trigger, you can use one of them to preview the request body as well to send a test request to ensure they end up in Salesforce.

Use the “Send Request” button to make the call and create the lead on Salesforce. You will be prompted to confirm that you wish to send the the web request, after which you should get the 201 Created response from the API call.

Salesforce uses the email as an unique id, so that if you send a request for an existing user, you should get back a 204 No Content response, where the existing record would be updated with any changes.

Success!

Finally we can confirm that everything works correctly by looking up the lead in Salesforce.

Note! The Salesforce access tokens expire frequently, and therefore need to be updated. Otherwise, requests will fail due to lack of authorization.

  • You can set up a cron job to update the access token more frequently, enabling you to set the webhook campaign to "Send Automatically."
  • If that's not accessible to you, we recommend setting your campaign to "Queue Draft" and approve sends in bulk manually. When you see that drafts are failing due to unauthorized requests (you'll see a 401 Unauthorized error message), you'll have to update the access token for the webhook.

Also note: Salesforce limits the number of requests you can make in a 24-hour window. Exceeding it will cause all subsequent requests to fail. The limit is tied to number of account licenses.

Need a hand? Let us know! We're happy to help.