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
Koustubh MasurkarKoustubh Masurkar 

Test class is not increasing the code coverage of trigger

I have written a trigger to count open cases of contacts. I wrote a test class for the trigger, but the code coverage remains 0%. What am I doing wrong here? Any help is appriciated.

This is my trigger:
trigger CountOpenCases on case (after insert, after update) { 	//Event after insert and after update
  List<Case> caseList = trigger.new;							//Assign cases to a list
  set<Id> contactIdSet = new set<Id>();							//Create set to store contact id
  for(Case cs: caseList ){										//Iterate over cases
     contactIdSet.add(cs.contactId);							//Add contact id from cases to set
  }
  if(contactIdSet.size() > 0){									//If set has elements
    List<contact> lstcontact = [select id, (select id from Cases where status!= 'Closed') from contact where id IN: contactIdSet ];	//Create list and query closed cases id and account id
       if(lstcontact.size() > 0){								//If query returns results
          for(contact acc: lstcontact)							//Iterate over result list
          { 
             acc.Open_Cases__c = acc.cases.size(); 				//Assign size of list to variable which counts open cases
          }
          update lstcontact;									//Update the contact
       }
  }
}

This is my test class:
@isTest
public class CountOpenCasesTestClass {
    static testmethod void myTest(){
        
        Account acc = new Account(Name = 'TestAccount1');
        insert acc;
        Id accid = acc.Id; //get account id
        Contact con = new Contact();
        con.LastName = 'TestContact1';
        con.AccountId = accid; //insert account id
        insert con;
        Case c = new Case(subject='Case of Test Class');
        Id cid = c.Id; //get case id
        c.Status = 'New';
        c.ContactId = cid; //insert contact id
        insert c;
    }
}


 
Best Answer chosen by Koustubh Masurkar
Khan AnasKhan Anas (Salesforce Developers) 
Hi Koustubh,

Greetings to you!

You are not assigning a correct Id to case ContactId. You need to use c.ContactId = con.Id instead of c.ContactId = c.Id. Please try below code:
@isTest
public class CountOpenCasesTestClass {
    
    static testmethod void myTest(){
        
        Account acc = new Account(Name = 'TestAccount1');
        insert acc;

        Contact con = new Contact();
        con.LastName = 'TestContact1';
        con.AccountId = acc.Id; //insert account id
        insert con;
        
        Case c = new Case();
        c.subject='Case of Test Class';
        c.Status = 'New';
        c.ContactId = con.Id; //insert contact id
        insert c;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Sunil RathoreSunil Rathore
Hi Koustubh,

Please remove line 13 because you cannot get id before insertion of the record. You can only use the id after the insertion of the record. Everything looks correct in your test class. 

​​​​​​​Please make sure while insertion of data from test class, all the required field should contain values. Check all the required field is mentioned in the test class.

Hope this will help you. If does then please mark it the best answer so it can also help others.

Many Thanks,
Sunil Rathore
Khan AnasKhan Anas (Salesforce Developers) 
Hi Koustubh,

Greetings to you!

You are not assigning a correct Id to case ContactId. You need to use c.ContactId = con.Id instead of c.ContactId = c.Id. Please try below code:
@isTest
public class CountOpenCasesTestClass {
    
    static testmethod void myTest(){
        
        Account acc = new Account(Name = 'TestAccount1');
        insert acc;

        Contact con = new Contact();
        con.LastName = 'TestContact1';
        con.AccountId = acc.Id; //insert account id
        insert con;
        
        Case c = new Case();
        c.subject='Case of Test Class';
        c.Status = 'New';
        c.ContactId = con.Id; //insert contact id
        insert c;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer