+ Start a Discussion
Sidharth Panda 5Sidharth Panda 5 

Test Class for After Insert Trigger

Test Class for After Trigger:


trigger Silverpop_Email_Activity_AfterInsertTrigger on silverpop__Silverpop_Email_Activity__c (after insert) {
  SkipBoolean__c skipBoolean = SkipBoolean__c.getOrgDefaults();
  Boolean triggerUnSkip = skipBoolean.Bypass_Trigger__c;
  List<Account> lstacct = new List<Account>();
  Database.SaveResult[] results;
  Set<Id> contactid = new Set<Id>();
  For(silverpop__Silverpop_Email_Activity__c emlacty:[select id,silverpop__Contact__c,silverpop__Status__c from silverpop__Silverpop_Email_Activity__c where Id IN:Trigger.new]){
      if (emlacty.silverpop__Status__c == 'Hard Bounce'){
          contactid.add(emlacty.silverpop__Contact__c);
      }    
  }
  if(triggerUnSkip){
      if(trigger.isAfter && trigger.isInsert){
          for(Contact con:[Select accountid from contact where Id IN :contactid]){       
            for(Account acc:[select Id,Player_Invalid_email__c from Account where Id=:con.accountid]){   
                acc.Player_Invalid_email__c = true;
                lstacct.add(acc);
            }
          } 
        if(lstacct.size() > 0){
           try{
               results = Database.update(lstacct, false);
           }
           catch(Exception e){
               for(Database.SaveResult result : results) {
                       if (!result.isSuccess()){
                           for(Database.Error err : result.getErrors()){
                               System.debug('Error: '+ err.getStatusCode() + ' ' + err.getMessage());}
                       }
                }   
           }
         }
      } 
        
   }
}
Leo10Leo10
Hi Sidharth,
Did you try with IsTest(SeeAllData=true)?

Thank you,
Sidharth Panda 5Sidharth Panda 5
@isTest
public class Silverpop_Email_Activity_Test {
public static testMethod void emailactivity(){
       Account acc = TestClassHelper.createPlayerAccount();
       insert acc;
       test.startTest();
           skipBooleanTest.skipBoolean();
           silverpop__Silverpop_Email_Activity__c emlact=new silverpop__Silverpop_Email_Activity__c();
           emlact.Name='name';
           emlact.silverpop__Status__c='Hard Bounce';
           emlact.silverpop__Contact__c=[select PersonContactId FROM account where ID =:acc.Id].PersonContactId;
           insert emlact;
       test.stopTest();
   }
}