function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Susan LinckSusan Linck 

Trigger in Sales Cloud to send email via Marketing Cloud

I need a trigger to send an email from SF to Exact Target using the integration.  The documentation for the integration provides the following code for the trigger- but it is wrong, or at least can't get it to save.
Object Name.trigger Trig_Object Name on Object Name (after insert, after update) {
et4ae5.triggerUtility.automate('Object Name'); }
For example:
trigger Trig_Lead on Lead (after insert, after update) {
et4ae5.triggerUtility.automate('Lead');}
This will be on the Contact object for a "Consumer" RT, so I think I need to have it be something like this:
 trigger Trig_Contact on Contact (after insert, after update) {
    for(Contact consumer : Trigger.new) {
    if (consumer.Type == 'Consumer')
    {et4ae5.triggerUtility.automate('Contact');
}}}
Do I need the for loop?   
The use case is when a new Contact is created with a WFR to send an acknowledging email to the Contact (for a warranty registration).
This is my first crack at writing a trigger, I'm the Admin.  I haven't seen how to use something like et4ae5.triggerUtility.automate. 
Thanks.
Best Answer chosen by Susan Linck
Josiah KaiserJosiah Kaiser
Hi Susan,

Getting the Custom APEX Trigger to save is certainly not as easy as they make it seem in the documentation, is it? I had to fight with this quite a bit too.

One thing that is not mentioned in the documentation is that you have to create Custom APEX Class that is a test class to test your trigger code in addition to the trigger code itself.

Here is a copy of the test class I used when I created my lead trigger:
/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class Trig_LeadTest {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        Lead testLead = new Lead(LastName = 'Test Lead', Email = 'help@fanmailmarketing.com');
        insert testLead;
        testLead = [select Id, LastName from Lead where id = :testLead.Id];
        System.assertEquals(testLead.LastName, 'Test Lead');
    }
}

As for your trigger code itself, I would suggest simplifying it so that it just applies to all Leads and then just set the criteria for the triggered send when you create the triggered send. Here is my trigger code.
trigger Trig_Lead on Lead (after insert, after update) {
	et4ae5.triggerUtility.automate('Lead');
}

So, save the test class first, then the trigger. If you are deploying via the Force.com IDE then you can deploy them as part of the same package.

Once they are deployed you can go to the Marketing Cloud tab and then to the "Configure..." link and you will see the "Triggered Sends" option. You will create your whole triggered send definition from here, including selecting the email and delivery profile and setting the conditions for when to send an email. So this is where you would set the criteria for Type = Consumer.

Once you activate your triggered send in salesforce you will see the send definition show up your Marketing Cloud account in the Triggered Sends folder as usual.

Hope this helps!

~Josiah Kaiser

All Answers

Josiah KaiserJosiah Kaiser
Hi Susan,

Getting the Custom APEX Trigger to save is certainly not as easy as they make it seem in the documentation, is it? I had to fight with this quite a bit too.

One thing that is not mentioned in the documentation is that you have to create Custom APEX Class that is a test class to test your trigger code in addition to the trigger code itself.

Here is a copy of the test class I used when I created my lead trigger:
/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class Trig_LeadTest {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        Lead testLead = new Lead(LastName = 'Test Lead', Email = 'help@fanmailmarketing.com');
        insert testLead;
        testLead = [select Id, LastName from Lead where id = :testLead.Id];
        System.assertEquals(testLead.LastName, 'Test Lead');
    }
}

As for your trigger code itself, I would suggest simplifying it so that it just applies to all Leads and then just set the criteria for the triggered send when you create the triggered send. Here is my trigger code.
trigger Trig_Lead on Lead (after insert, after update) {
	et4ae5.triggerUtility.automate('Lead');
}

So, save the test class first, then the trigger. If you are deploying via the Force.com IDE then you can deploy them as part of the same package.

Once they are deployed you can go to the Marketing Cloud tab and then to the "Configure..." link and you will see the "Triggered Sends" option. You will create your whole triggered send definition from here, including selecting the email and delivery profile and setting the conditions for when to send an email. So this is where you would set the criteria for Type = Consumer.

Once you activate your triggered send in salesforce you will see the send definition show up your Marketing Cloud account in the Triggered Sends folder as usual.

Hope this helps!

~Josiah Kaiser
This was selected as the best answer
Susan LinckSusan Linck
Thank you Josiah.  This is really helpful.  
Vicky DabrowskiVicky Dabrowski
Hi Josiah - 

I used the code you posted above and I was able to successfully Activate the triggered sends I set up on the Lead object.  I'm trying to create the Apex class and trigger for the Campaign Members object, but haven't been able to edit the code above to save correctly in order to activate it.  

I've posted this question (https://developer.salesforce.com/forums/ForumsMain?id=906F0000000MJ6u) but was hoping you may have some insight based on your reply to this post.

Thank You,
Vicky
Brian KarcinskiBrian Karcinski
This was very helpful.  I did have to add Company field to the test code for it to work for me as it is a required field on lead.
Shekhar Dautpure AppirioShekhar Dautpure Appirio
This can be done using salesforce data event entry source now (back then it was not available )