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
Sebastian PageSebastian Page 

Why Some lines of apex trigger is not covered code coverage

Hi Expert,
I have business requirment for Case merge. Whenever Case (ticket) is creating by Email Case and Ticket is duplicate. then Case (ticket ) status should be change closed sucessful (duplicate). And all comments and attachemts will be add into parrent ticket.
 my apex trigger working smoothly as per requirment but when i create test class i got 48 % code coverage. Please assist  me where is mistak.

I am adding Screen shoot where i getting code covering problem.

User-added image

Here my apex trigger- 
trigger EMailToCaseMerge on Case (After insert) {
     List<string> lstSubject=new List<String>();
    Set<Id>AccId =new Set<Id>();
    Set<Id>RecId = new Set<Id>();
    String casId='';    
    List<String> dupCas=new List<String>(); 
    list<String> getuserId =CaseMergeEmail.getuserIdlst();
    system.debug('userIds'+getuserId);
    List<Case> lstUpdateCase= new list<case>();
     for(Trigger_Control__c tc:Trigger_Control__c.getall().values()){ 
         if(tc.Enable_zGlobalCaseMerge__c==true)
         {
    for(Case cs :Trigger.new)
        {
           
            AccId.add(cs.AccountId);
            lstSubject.add(cs.Subject);
            RecId.add(cs.recordTypeId);
        }
    
   if(userInfo.getUserName()=='administrator@helpdesk.org.newapi'){
       List<Case> getCaslst=[select status,parentId, Prevent_Closure_Email__c,Prevent_CaseComment_Notification__c,id from case where accountId in:AccId and subject in:lstSubject and isClosed=false and recordtypeId in:RecId  and status!='Escalated' order by createddate];
       if(getCaslst.size()>0){
       system.debug('getCaslst[0].id'+getCaslst[0].id);
       casId=getCaslst[0].id;
       for(Case cs:getCaslst) 
       {    
           if(cs.id!=getCaslst[0].id)  
           { 
              cs.parentId=getCaslst[0].id; 
            cs.Prevent_Closure_Email__c=true;  
            cs.Prevent_CaseComment_Notification__c=true;  
            cs.status='Closed (Duplicate)';
           
        lstUpdateCase.add(cs);
       dupCas.add(cs.Id); 
    }
    
    }
           

    update lstUpdateCase;
        
        if(dupCas.size()>0)           {
              CaseMergeEmail.casAttachment(dupCas,casId);  
            CaseMergeEmail.CasCommnet(dupCas,casId);
            //  CaseMergeEmail.TicketLogs(trigger.new,casId);  
           }
       }  
     }
    }
   }

 }

And this is a test class
 
@IsTest
public class CaseMergeHandlerTest {

    static testMethod void testMerge(){
        List<String>DupCase =new List<String>();
        Account testAccount = new Account(Name='Test Company Name123');
        insert testAccount;

        Contact con  = new Contact(LastName='Testcont' ,Email='sss@test.com', AccountId=testAccount.Id);
        insert con;
        Trigger_Control__c tc=new Trigger_Control__c();
        tc.name='testCustumer';
        tc.Enable_zGlobalCaseMerge__c=true;
        insert tc;
         User u;
        User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];        
        System.runAs (thisUser) {
            Profile p = [SELECT Id FROM Profile WHERE Name='CH_Tier1-Partner'];
            u = new User(alias = 'tstwy', email='testmail@tst.com', 
                    emailencodingkey='UTF-8', lastname='testL', 
                    languagelocalekey='en_US', 
                    localesidkey='en_US', profileid = p.Id, ContactId = con.Id,
                    timezonesidkey='America/Los_Angeles', 
                    username='testmailxxx@tst.com');
            insert u;  
        } 
        
 System.runAs (u) {
      List<RecordType> listRecType = [select Id from RecordType where sObjectType = 'Case' And Name = 'Pasadena_Ticket'];
        List<Case>lstcase =new List<Case>();

        for(Integer i=0; i <2; i++){
           lstcase.add(new Case( Prevent_Closure_Email__c=true,Prevent_CaseComment_Notification__c=true,AccountId=testAccount.Id, ContactId = con.Id,Origin='Email', subject='test',recordtypeId= listRecType[0].Id ,Status='New',ownerid='00Gd0000001QYQU'));
                    }
        insert lstcase;

        DupCase.add(lstcase[0].Id);
        DupCase.add(lstcase[1].Id);

        Attachment attach=new Attachment();
        attach.Name='test.html';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        attach.Body=bodyBlob;
        attach.ParentId=lstcase[0].Id;
        insert attach;

        List<Attachment> attachments=[SELECT Id, Name, ParentId FROM Attachment WHERE Parent.Id=:lstcase[0].Id];
        System.assertEquals(1, attachments.size());  //Sanity check
        
       
              caseComment newcmt=new casecomment();
                newcmt.ParentId=lstcase[0].Id;
                newcmt.CommentBody='Test comments';
               insert newcmt;
        
        List<caseComment> lstCaseComments=[SELECT Id, CommentBody, ParentId FROM caseComment WHERE Parent.Id=:lstcase[0].Id limit 1];
        System.assertEquals(1, lstCaseComments.size());  //Sanity check*/

        Test.startTest();
        CaseMergeEmail.casAttachment(DupCase, lstcase[0].Id);
        CaseMergeEmail.CasCommnet(DupCase, lstcase[0].Id);
        //CaseMergeEmail.TicketLogs(lstcase);
        Test.stopTest();

       attachments=[SELECT Id, Name, ParentId FROM Attachment WHERE Parent.Id=:lstcase[0].Id];
        System.assertEquals(2, attachments.size());  //real check - did we duplicate the attachment
       System.assertEquals(attachments[0].Name, attachments[1].Name);
       
         lstCaseComments=[SELECT Id, CommentBody,ParentId FROM CaseComment WHERE Parent.Id=:lstcase[0].Id];
         System.assertEquals(2, lstCaseComments.size()); 
        System.assertEquals(lstCaseComments[0].CommentBody, lstCaseComments[1].CommentBody);
 }
    }
}

 
AnudeepAnudeep (Salesforce Developers) 
Hi Sebastian, 

Based on the lines that are not covered I am suspecting it is to do with the username on the User. There is an if condition in your trigger that is looking for administrator@helpdesk.org.newapi as the username
 
if(userInfo.getUserName()=='administrator@helpdesk.org.newapi'){

I recommend setting the same value in your test class as well
 
u = new User(alias = 'tstwy', email='testmail@tst.com', 
                    emailencodingkey='UTF-8', lastname='testL', 
                    languagelocalekey='en_US', 
                    localesidkey='en_US', profileid = p.Id, ContactId = con.Id,
                    timezonesidkey='America/Los_Angeles', 
                    username='administrator@helpdesk.org.newapi');
            insert u;

Let me know if this helps, if it does, please mark this answer as best so that others facing the same issue will find this information useful. Thank you