marketing-cloud-how-to-create-triggered-sends-ccg-header.jpg

Tipps & Tricks

Triggered Sends

Salesforce Marketing Cloud: How to create Triggered Sends

Speed is of the essence for a digital marketer. And often there are scenarios in which you need to send a message immediately to a subscriber after they take some sort of action. Whether it’s a welcome message, an order or registration confirmation - utilizing the Salesforce Marketing Cloud’s Triggered Sends functionality can help deliver this time-critical messages to your subscriber’s inbox. We give your developer team a detailed overview of how to set up and use triggered sends with Salesforce Marketing Cloud.

 

What is Triggered sends?

A triggered Send is an email communication sent to an individual subscriber in response to a subscriber action. Triggered Sends trigger a Salesforce Marketing Cloud email from Salesforce using the Salesforce Marketing Cloud integration action. Marketing Cloud SOAP API is the preferred method to integrate triggered send emails with your business applications due to its superior error-handling capability and the ability to retrieve tracking data. 

Automatically send relevant, timely, personalized messages using triggered sends. Welcome emails to new customers or thank you notes for purchases are examples of how triggered sends are commonly used.

 

Trigger Marketing Cloud Emails from Salesforce

A triggered email is a message that Marketing Cloud sends to an individual subscriber in response to a subscriber action. We can create a triggered send definition to be referenced by an external application or development environment to send a predetermined message to subscribers that complete a certain action, such as signing up on your email list or requesting more information. Reference the triggered send definition in triggered send request.

We can use triggered sends API in Salesforce to create and send automated messages to Salesforce object record ( like: contacts, leads, person accounts, custom object etc.),  when object records are created or updated. In order to use this service, configure a triggered send definition in Email Studio.

We can customize the From Address and To Address when using the Triggered Sends API. The standard approach is good for the standard objects when we have fixed fields for Email address and content. So, sometimes based on the requirement we have to go with a Custom API solution.

A triggered Sends consists of two components that we create in Marketing Cloud: 

  • Content 
  • Interaction

So, first we need to Create a triggered send definition (Click Interactions tab ==> Triggered Sends) in Marketing Cloud to respond to a subscriber action with an email message. Create your email content before creating the interaction. After completing this procedure, start the interaction. Marketing Cloud will generate a unique CustomerKey for every TriggerSend, we will use this CustomerKey later when we will implement the apex API callout in Salesforce.

After setting up your triggered emails in Salesforce Marketing Cloud we just need to implement an API callout in Salesforce apex which will call the Marketing Cloud Trigger Send API to run the email process.

So, every time when triggered send API will be triggered from Salesforce, an email will be sent from Marketing Cloud.

Triggered sends Limitation: Triggered sends do not support bulk updates or inserts. So, for now we are using a workaround by creating a apex Batch job.

 

With the help of the Marketing Cloud Connect we can use many features of triggered Sends within the Sales or Service Clouds which are as follows: 

  • Create and manage Marketing Cloud triggered Sends
  • View existing triggered Sends across different clouds 
  • Analyse tracking data for triggered Sends across multiple clouds
  • Add subscribers to data extensions for additional marketing opportunities 
  • If in case the user has selected none of the data extensions, then the subscriber will get added to the All Subscribers list in the Marketing Cloud. And if the user has activated, then it will not get added to the All Subscribers list.
  • You are required to have access to All Subscribers list in the Salesforce Marketing Cloud. 
  • Make sure that the triggered Sends are activated in the Marketing Cloud. 
  • The email which is incorporated within the triggered Sends have to go through the validation process in the Marketing Cloud.  
  • Triggered Sends executes only with the events that take place after the operations.  
  • Once you have created the triggered Sends in Sales Cloud, and now if you will edit the triggered sends in Marketing Cloud, it will change the ID, which will prevent updates.
  • Triggered Sends are created with the Apex code. You have to create and test triggers in a Salesforce environment.  
  • Triggered Sends does not incorporate mass updates or inserts.
  •  Marketing cloud connect is controlled by SOQL query limits. 
  • There is a limit. You cannot execute more than 50 triggered Sends within each transaction.  

 

CONFIGURE THE OBJECTS AND TRIGGERS WITH THE MARKETING CLOUD CONNECT

These are the following steps to create and execute triggers & enable objects with Marketing Cloud Connect. 

  1. From Sales or Service Cloud, click Setup. 
  2. Under the Build section, click on Customize. 
  3. Then select your object. 
  4. Click Triggers. 
  5. Click New. 
  6. In the Apex window, select Is Active. 
  7. Enter the code with your desired Object Name. 
  8. Save the trigger. 
  9. The trigger has been created for the object. 
  10. Click the Marketing Cloud tab. 
  11. Click Configure Marketing Cloud Connect. 
  12. Click Edit Settings. 
  13. Under the Triggered Sends section, select Restriction to set user permissions and restrictions. 
  14. Under the Triggered Sends section, add Enabled Objects to be used for triggered Sends. 
  15. Save the settings and selections. The restrictions are all set and objects are enabled. 

Triggered Sends use Apex code. Write and test triggers in a Salesforce developer environment, sandbox, or trial org before publishing to production.

 

Setup Connection

We are now going to explain a complete custom solution, which will work for standard and custom objects. This is always the first step in any API-based integration is getting an OAuth access token to authenticate your calls. We have to retrieve the access token from Marketing Cloud. To obtain an OAuth token, we will use HTTP POST requestToken API and specify the clientID and clientSecret in the request body.

To use the Marketing Cloud API, we need a client ID and secret. To get these credentials, create an installed package in Marketing Cloud and add an API Integration component. Once we have client ID and secret credentials, we can use them to acquire an OAuth access token directly from the API authentication service.

Custom Setting: we can use a custom setting to save the integration configuration, Ex: clientId, clientSecret and customerKey(External Key) of Triggered Send.

 

Example Request to get  an OAuth access token: 

POST auth.exacttargetapis.com/v1/requestToken

Content-Type: application/json

{

   "clientId": "ksjzvytv7ukqtfn3x2qdyfsn",

   "clientSecret": "************"

}

 

Example Response:

{

     "accessToken": "************************",

     "expiresIn": 3600

}

 

Salesforce Apex Code:

HttpRequest req = new HttpRequest();

req.setMethod('POST');

req.setHeader('Content-Type', 'application/json');

req.setEndpoint('https://auth.exacttargetapis.com/v1/requestToken');

// fetch ClientId from Custom setting.

String clientId = 'ksjzvytv7ukqtfn3x2qdyfsn';

// fetch clientSecret from Custom setting.

String clientSecret = '************************'; 

/*We can create an utility apex class for making Request Body. Here in this example i am using

getExactTargetRequestTokenBody method which return the request body JSON. */

req.setBody(ApiRequestUtility.getExactTargetRequestTokenBody(clientId, clientSecret));

Http http = new Http();

HTTPResponse res = http.send(req);

As per the development best practice we have to add error handling and validation to throw error if API response could not retrieve access token from the Marketing Cloud.

 

Triggered Sends - API Integration

Now we will discuss the Triggered Send API callout development in Salesforce. We need some required field mapping for this API callout as the EmailSubject property and Subscriber key on the triggered send definition.  If you don't pass the new subject to the triggered send definition, it retains the old subject for that definition.

Example Request Using Customer Key:

POST /messaging/v1/messageDefinitionSends/key:cust_key/sendContent-Type: application/jsonAuthorization: Bearer YOUR_ACCESS_TOKEN{    "From": {        "Address": "ccg@exacttarget.com",        "Name": "CCG Test"    },    "To": {        "Address": "test@ccg.com",        "SubscriberKey": "test@example.com",        "ContactAttributes": {            "SubscriberAttributes": {                "City": "Frankfurt",                "Country": "DE"            }        }    },    "Options": {        "RequestType": "SYNC"    }}

 

Example Response:

HTTP/1.1 202 Accepted{   "requestId": "e2ddb203-ea53-4843-b2d4-9f8c0c862913",   "responses": [   {      "recipientSendId": "e2ddb203-ea53-4843-b2d4-9f8c0c862913",      "hasErrors": false,      "messages": ["Queued"]   }]}

 

Salesforce Apex Code:

HttpRequest req = new HttpRequest();

req.setMethod('POST');

req.setHeader('Content-Type', 'application/json');

// fetch CustomerKey from the Custom setting.

String customerKey = 'Test_Custmer_Key';

String url = 'https://www.exacttargetapis.com/messaging/v1/messageDefinitionSends/key:'+customerKey+'/send';

req.setEndpoint(url);

//Pass the access token in HTTP header Authorization parameter. 

req.setHeader('Authorization','Bearer ' + [access token here from the first request token API response] );

req.setHeader('Content-Type','application/json');

/*We can create an utility apex class for making Request Body. Here in this example i am using createTriggeredSendRequestBody method which return the request body JSON for messageDefinitionSends API POST callout */

String requestBody =ApiRequestUtility.createTriggeredSendRequestBody();

req.setBody(requestBody);

Http http = new Http();

HTTPResponse res = http.send(req); 

if (res.getStatusCode() < 200 || res.getStatusCode() >= 300) {

System.debug(LoggingLevel.WARN, 'Message definition send API could not forward data to Marketing Cloud: ' + res.toString());

throw new ServerSideException('Forwarding order data to marketing cloud failed with status code '+ res.getStatusCode() + ' and status ' +res.getStatus());

}

 

Similar as we did in the previous requestToken API callout, we also have to add error handling and validation to throw error if it could not send object data to the marketing cloud due to any callout exception.

If you need help with your marketing automation or Salesforce Marketing Cloud get in contact with us.

Get in contact!
 

Teilen Sie diesen Artikel

Beliebteste Artikel