Sunday, January 21, 2018

Test Class for your Marketing Cloud Trigger

When using the Salesforce Marketing Cloud Connector to enable Marketing Cloud access from Sales and Service Clouds, folks often want to perform a triggered send when a specific event occurs. To do so, one must create a trigger on the desired object.  For example, let's say you allow loyalty signups from your web site. Upon signup, you create a new contact record in Salesforce Sales Cloud. Best practice is to send an email to the user right away. This gives users a level of confidence they actually signed up. You might also want to send a verification link, etc. However, let's keep it simple.

First, create your trigger on the Contact object. This must be named "Trig_Contact". Follow these instructions.

OK. Now I'm assuming you did this in a dev org since you can't create it in production. At this point, you're probably wanting to deploy your simple trigger to production so you can go on and create your send definition. Here's where you run into a problem. Salesforce kind of forgets to mention you need a test class.  To make matters worse, you might not be an APEX developer... so this is going to be fun.

If you want some background on test classes, read this help article.

Back already? Awesome. Let's get to it and create that test class.

  1. From Setup, enter Apex Classes in the Quick Find box, then select Apex Classes and click New.
  2. In the class editor, add the code below as the class definition, and then click Save.

@isTest
private class Trig_ContactTest {
static testMethod void myUnitTest() {
// TO DO: implement unit test
Contact testContact = new Contact(LastName = 'Test Contact', Email ='help@chrisbeckworth.com');
insert testContact;
testContact = [select Id, LastName from Contact where id = :testContact.Id];
System.assertEquals(testContact.LastName, 'Test Contact');
}
}
Great. Your all set. Now you need to make a changeset with your Contact trigger and test class. Upload that changeset to Production. Go into Production and deploy the changeset. Make sure, when deploying, to select the last option to only test items in the changeset.

Here's how to set up the send definition.
Here's more info on triggered sends in general.

Test Class for your Marketing Cloud Trigger

When using the Salesforce Marketing Cloud Connector to enable Marketing Cloud access from Sales and Service Clouds, folks often want to perf...