+ Start a Discussion
Gelu G.Gelu G. 

Apex Test Class failing for triggered send

Hello all,
Our Sales Cloud and Marketing Cloud are connected via the Marketing Cloud Connector.  I am trying to use the triggered send feature of Sales Cloud and send a Marketing Cloud email when a person account is created in the Sales Cloud.

I have the following trigger and test class set up on my Sandbox environment.
trigger Trig_Account on Account (after insert, after update) {
    if (!Test.isRunningTest()){
        et4ae5.triggerUtility.automate('Account');
    }
}

@isTest
private class Trig_AccountTest {
    static testMethod void myUnitTest() {
        Account testAccount = new Account(LastName = 'Test Account', PersonEmail= 'testclass@test.com');
        insert testAccount;
        testAccount = [select Id, LastName from Account where id = :testAccount.Id];
        System.assertEquals(testAccount.LastName, 'Test Account');
    }
}
The test class executes without any problems but the code coverage on the trigger remains at 50%(1/2).  I won't be able to deploy this to production because it needs to be at 75% at least.  I can't see what I am doing wrong here so any help will be apprecaited.
Best Answer chosen by Gelu G.
Gelu G.Gelu G.
Hi,
Solved this by writing the trigger and test class as follows:
 
trigger Trig_Account on Account (after insert, after update) {
    List<Account> newPersonAccounts = new List<Account>();
       for (Account account : Trigger.new) {
          if (account.IsPersonAccount) {
          //Triggered Email to Marketing Cloud for Person Account 
          if(!Test.IsRunningTest()) {  
            et4ae5.triggerUtility.automate('Account');
          }
        }
    }
}
 
@isTest
    private class Trig_AccountTest {

    static testMethod void myUnitTest() {
        String MyRecordTypeId= [select Id from RecordType where (Name='Person Account') and (SobjectType='Account')].Id;
      
        Account testAccount = new Account(LastName = 'Test Account', PersonEmail = 'test@testemail.com', RecordTypeID = MyRecordTypeId);

        insert testAccount;

        testAccount = [select Id, LastName from Account where id = :testAccount.Id];

        System.assertEquals(testAccount.LastName, 'Test Account');

    }

}

 

All Answers

v varaprasadv varaprasad
Hi Gelu,

Remove the below  line and check once:

if (!Test.isRunningTest()){ }





Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.


Salesforce Freelance Consultant/Developer/Administrator/Trainer
@For Salesforce Project Support: varaprasad4sfdc@gmail.com


Salesforce latest interview questions and training videos :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1


Thanks,
V Varaprasad
Email: varaprasad4sfdc@gmail.com
Mobile No : +91 9393934143

 
Gelu G.Gelu G.
Hi Varaprasad,
If I remove that, I get the error below.  We have person account enabled.  Does it mean support have to enable it for triggered send?


System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Trig_Account: execution of AfterInsert

caused by: et4ae5.MCBaseException.InvalidParameterException: Whoops! Marketing Cloud Connect does not have access to the selected object. Contact your administrator to enable objects for Marketing Cloud Connect triggered sends.

Class.et4ae5.triggerUtility.automate: line 30, column 1
Trigger.Trig_Account: line 2, column 1: []
v varaprasadv varaprasad
Please refer below links once:


https://salesforce.stackexchange.com/questions/181699/marketing-cloud-triggered-send
https://salesforce.stackexchange.com/questions/210508/unable-to-deploy-trigger-for-triggered-sends-to-activate
Gelu G.Gelu G.
Hi,
Solved this by writing the trigger and test class as follows:
 
trigger Trig_Account on Account (after insert, after update) {
    List<Account> newPersonAccounts = new List<Account>();
       for (Account account : Trigger.new) {
          if (account.IsPersonAccount) {
          //Triggered Email to Marketing Cloud for Person Account 
          if(!Test.IsRunningTest()) {  
            et4ae5.triggerUtility.automate('Account');
          }
        }
    }
}
 
@isTest
    private class Trig_AccountTest {

    static testMethod void myUnitTest() {
        String MyRecordTypeId= [select Id from RecordType where (Name='Person Account') and (SobjectType='Account')].Id;
      
        Account testAccount = new Account(LastName = 'Test Account', PersonEmail = 'test@testemail.com', RecordTypeID = MyRecordTypeId);

        insert testAccount;

        testAccount = [select Id, LastName from Account where id = :testAccount.Id];

        System.assertEquals(testAccount.LastName, 'Test Account');

    }

}

 
This was selected as the best answer