+ Start a Discussion
Ram Srivastav 7Ram Srivastav 7 

Code coverage issue

Hi, I have defined one trigger which used to intimate the user with acknowledgement mail. To prevent the same email twich i  do have defined one apex class. Issue is my test class is containing just 52% code coverage due to which i am not able to deploy my code change into the production. Please help

Apex class code snippet: Code coverage 100%

public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}

Apex Trigger code snippet: 

trigger sendEmail on Case (after insert,after update) {
   Public static Boolean InitialEmail =false;
   Public static Boolean FinalEmail =false;
 
  if(checkRecursive.runOnce()){
    
     for(Case c:trigger.new) {
    
      system.debug('outside'+c.Send_Email_to_Contact__c);
    
       if ((trigger.isInsert || (trigger.oldMap.get(c.Id).Send_Email_to_Contact__c != c.Send_Email_to_Contact__c)) && (c.Send_Email_to_Contact__c && !c.Do_not_Send_Email__c  && c.Email_of_Complainant__c!=null && c.Status!='Completed')) {
               system.debug('??????'+c.Send_Email_to_Contact__c);
           system.debug('Ram');
               sendEmailMessage(c.id,c.ownerId,c.Internal_Comments__c,c.Email_of_Complainant__c,c.Site_Details__c,c.CaseNumber,c.Case_Customer_Reference__c );
        }
            
              if ((trigger.isInsert || (trigger.oldMap.get(c.Id).Status != c.Status)) && (c.Final_email_to_contact__c && !c.Do_not_Send_Email__c  && c.Email_of_Complainant__c!=null && c.Status=='Completed')) {
            
                system.debug('****************'+c.Send_Email_to_Contact__c);
            
                sendFinalEmailMessage(c.id,c.ownerId,c.Internal_Comments__c,c.Email_of_Complainant__c,c.Site_Details__c,c.CaseNumber,c.Case_Customer_Reference__c);
        }
      
       }
}


Apex test class: code coverage just 52%

@isTest
public class TestClass_SendEmail{
static testmethod void SendEmails(){
Case cs = new Case(Status ='Outstanding',Complaint_Type__c ='Complaint',Query_Cause_Code__c='Disabled',Sub_Cause_Code__c ='Disabled',Send_Email_to_Contact__c=true,Final_email_to_contact__c=true,Email_of_Complainant__c='freak.abhinav@gmail.com');
insert cs;

cs.Status='Completed';
update cs;
}
Ravikant Saini 1Ravikant Saini 1
Hi Ram,
              I think you forget to fill this field.before insert.
c.Do_not_Send_Email__c = false;