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
devloper sfdcdevloper sfdc 

Getting error List index out of bound 0 and i got 36% code coverage

Hello All, 

I am getting error in test class is ''List index out of bound 0" . And I getting 36% code coverage in my test class. Please help me both are class are there 

apex class.
public class CaseMergeApexClass {
    Public String textEnter {get;set;}
    public List<sObjectWrapper> wrappers {get;set;} 
    public List<case> lstcas {get;set;}
    public String selectMasterCase {get;set;}
    public boolean addOnChkbx {get;set;}  
    public boolean isSelected{get;set;}
    public boolean flagmerged;
    Public String caseId {get;set;} 
    public List<case>mappsub {get;set;}
    public CaseMergeApexClass(ApexPages.StandardController stdController) {
       caseId = apexpages.currentpage().getparameters().get('id');  
       lstcas = new List<case>();
      wrappers=new List<sObjectWrapper>();
        
        flagmerged =true;
        mappsub =new List<case>();
        for(case cs:[select id,Account.name,Contact.name,RecordType.name,owner.name,subject from case where id=:caseId limit 1])
        {
          mappsub.add(cs);
        }
    }
    public void mergeCase()
    {
       
      list<Case>lstgetcas=[select zGloblal_Total_attachments__c, RecordType.name,CaseNumber,id,subject,account.name,contact.name from case where RecordType.name=:mappsub.get(0).RecordType.name and Contact.name=:mappsub.get(0).Contact.name and Subject like :'%'+mappsub.get(0).subject+'%' and status!='Closed Duplicate' ];
        
          
        for(case cs:lstgetcas)
        {
           
            if(flagmerged){
          wrappers.add(new sObjectWrapper(cs,addOnChkbx));
         }
        
    }
        
    
    
        flagmerged=false;
    } 
    public void selectForMaster()
    {
        lstcas.clear();
         for(sObjectWrapper wrap : wrappers){  
       /*Check if record is selected*/  
       if(wrap.isSelected==true){
         
           
         lstcas.add(wrap.cas);  
        
    }
    }  
    }
    
    public void mergedTicket()
    {
        list<case> updcase=new List<case>();
        list<caseComment> NewCaseCommet=new List<caseComment>();
        list<caseComment> lstComment= [select id ,commentbody,parentid from casecomment where parentId in:lstcas];
        for(casecomment cmt:lstComment)
        {
           caseComment newcmt=new casecomment();
           newcmt.ParentId=caseId; 
           newcmt.CommentBody=cmt.CommentBody; 
           NewCaseCommet.add(newcmt);
        }
       insert NewCaseCommet;
         List<attachment> newAttachment =new list<attachment>();
   List<attachment> lstAttachments=[select id,Body,Name,ParentId from attachment where parentId in:lstcas];
        for(attachment att:lstAttachments)
        {
            attachment newattch=new attachment();
            newattch.parentId=caseId;
            newattch.Body=att.body;
            newattch.name=att.name;
            newAttachment.add(newattch);
        }
       insert newAttachment; 
    closedDuplicate();
      urlre();   
    }
    
    public void closedDuplicate()
    {
        List<case> closedcase =new List<case>();
        for(case cs:lstcas)
        {
            cs.Status='Closed Duplicate';
            closedcase.add(cs);
        }
       update closedcase; 
        
    }
    
    public pagereference urlre()
{
caseId = apexpages.currentpage().getparameters().get('id');
pagereference pg =new pagereference('/'+caseId);

return pg;   
}

    public class sObjectWrapper
  {
    public boolean isSelected{get;set;}  
    public case cas{get;set;} 
        
    public sObjectWrapper(case cas,Boolean isSelected){  
    this.cas = cas;      
    this.isSelected = isSelected;  
     }  
    }
}

and my test class is
@isTest
public class CaseMergeApexClassTest {
    
      static testMethod void testMerge(){        
       
        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;
          list<case>lstcase =new LIst<case>();
             Case caseObj1 = new Case();
            caseObj1.AccountId = testAccount.Id;
            caseObj1.contactId=con.id;
            caseObj1.Status = 'Action-Automated Escalation';
            caseObj1.Origin ='Email';
          lstcase.add(caseObj1);
          
           Case caseObj2 = new Case();
            caseObj2.AccountId = testAccount.Id;
            caseObj2.contactId=con.id;
            caseObj2.Status = 'Closed Duplicate';
            caseObj2.Origin ='Email';
          lstcase.add(caseObj2);
       test.startTest(); 
        insert lstcase;

        
            string casId=lstcase[0].id;
        
        ApexPages.StandardController sc = new ApexPages.StandardController(lstcase[0]);
       
        CaseMergeApexClass testCon = new CaseMergeApexClass(sc);
        
          testCon.CaseId = lstcase[0].id ;
   
        PageReference pageRef = Page.CaseMergePage;
        pageRef.getParameters().put('id', String.valueOf(lstcase[0].id));
        Test.setCurrentPage(pageRef);
          testCon.addOnChkbx=true;
          testCon.closedDuplicate();
          testCon.textEnter='899898';
          testCon.selectMasterCase='89898';
         testCon.flagmerged=true;
         testCon.isSelected=true;
          
          testCon.urlre();
          testCon.mergeCase();          
          testCon.selectForMaster();
          testCon.mergedTicket();

      test.stopTest();
    }
}

​​​​​​​
Andrew GAndrew G
hi

List index out of bound 0   means that you have tried to access an empty list.  Or more correctly , that there is no data at position [0] in your list.  Best practice is to test that the list contains data prior to accessing it.

So in you code, you would need to test where you build your lists and then attempt to loop through them.  In this case, looking quickly, there is a Case Comment list but I see no Case comments created in your test data.

So code update could be:
list<caseComment> NewCaseCommet=new List<caseComment>();
        list<caseComment> lstComment= [select id ,commentbody,parentid from casecomment where parentId in:lstcas];
if(!lstComment.isempty()) {
        for(casecomment cmt:lstComment)
        {
           caseComment newcmt=new casecomment();
           newcmt.ParentId=caseId; 
           newcmt.CommentBody=cmt.CommentBody; 
           NewCaseCommet.add(newcmt);
        }
       insert NewCaseCommet;
}
And then in your Test code, insert some Case Comment test data.


Regards

Andrew