You need to sign in to do that
Don't have an account?

count number of contact from account . below is working code can anyone tell test class for this
Apex code
public class CountContactHandler {
public static void CountContactHelper(List<contact> newcontact, List<contact> oldcontact){
set<id> accIds= new set<id>();
if(newcontact != null){
for(Contact c : newcontact){
if(c.AccountId!=null){
accids.add(c.accountid);
}
}
}
if(oldcontact != null){
for(Contact c : oldcontact){
accids.add(c.accountid);
}
}
List<Account> accList = [Select Id, NoofContacts__c, (Select id from Contacts) from Account where Id IN :accIds];
if(accList!=null){
for(Account acc : accList){
acc.NoofContacts__c = acc.Contacts.size();
}
}
if(!accList.isEmpty()){
update accList;
}
}
}
Test class :
@isTest
public class CountContactHandlerTest {
@isTest
public static void CountContactHelpertest(){
//Create sample data
List<Account> accList = new List<Account>();
for(Integer i=1;i<=5;i++){
Account acc = new Account();
acc.Name='Test'+i;
acc.Industry='Energy';
accList.add(acc);
}
// insert data
Test.startTest();
insert accList;
Test.stopTest();
List<Contact> conList = [Select Id from Contact where AccountId =:accList[0].Id];
System.assert(conList!=null, 'Contact is not created');
}
}
public class CountContactHandler {
public static void CountContactHelper(List<contact> newcontact, List<contact> oldcontact){
set<id> accIds= new set<id>();
if(newcontact != null){
for(Contact c : newcontact){
if(c.AccountId!=null){
accids.add(c.accountid);
}
}
}
if(oldcontact != null){
for(Contact c : oldcontact){
accids.add(c.accountid);
}
}
List<Account> accList = [Select Id, NoofContacts__c, (Select id from Contacts) from Account where Id IN :accIds];
if(accList!=null){
for(Account acc : accList){
acc.NoofContacts__c = acc.Contacts.size();
}
}
if(!accList.isEmpty()){
update accList;
}
}
}
Test class :
@isTest
public class CountContactHandlerTest {
@isTest
public static void CountContactHelpertest(){
//Create sample data
List<Account> accList = new List<Account>();
for(Integer i=1;i<=5;i++){
Account acc = new Account();
acc.Name='Test'+i;
acc.Industry='Energy';
accList.add(acc);
}
// insert data
Test.startTest();
insert accList;
Test.stopTest();
List<Contact> conList = [Select Id from Contact where AccountId =:accList[0].Id];
System.assert(conList!=null, 'Contact is not created');
}
}
Can you confirm what excatly issue with your test class? Is it not covering the code or is it failing.
Thanks,
and test class:
Hope it helps you.
Is this called in after trigger context. Can you share the trigger as well for the same.
Thanks,
below is the trigger code
trigger CountContactsOnAccount on Contact (after insert, after update, after delete, after undelete) {
//call handler for best practice
if(Trigger.isinsert || Trigger.isupdate || trigger.isdelete || Trigger.isundelete){
CountContactHandler.CountContactHelper(trigger.new, trigger.old);
}
}
Can you try the below test class. It gave me 100% Coverage
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,