{
for(Contact con:newAcc)
{
accountIds.add(con.accountID);
}
this.updatingContactsCount();
} public void updateOperation(List<Contact> newAcc,Map<Id,Contact>oldMap)
{
for(Contact con : newAcc)
{
if(con.AccountId != null)
{
if(oldMap.get(con.Id).AccountId != con.AccountId)
{
accountIds.add(con.AccountId);
}
}
accountIds.add(oldMap.get(con.Id).AccountId);
}
this.updatingContactsCount();
} Public void updatingContactsCount() { for(Account acc:[SELECT Id,Name,Count_Contacts__c,Total_Sum__c,(Select Id,Sum__c from Contacts) from Account where Id IN: accountIds]) { acc.Count_Contacts__c = acc.Contacts.size(); acc.Total_Sum__c = 0; for(Contact con : acc.Contacts) { if(con.Sum__c != NULL) { acc.Total_Sum__c = acc.Total_Sum__c + con.Sum__c; } } lstAccountsToUpdate.add(acc); } UPDATE lstAccountsToUpdate; }}test class@isTestprivate class UpdatingAccountRelatedContactsCount_Test { static Testmethod void accountRelatedContacts() { List<Contact> conList = new List<Contact>(); Account Acc = New Account(); Acc.Name = 'srikanth'; insert Acc; Contact con = new Contact(); con.LastName = 'Test'; con.AccountId = Acc.Id; con.Sum__c = 100; conList.add(con); Contact con1 = new Contact(); con.LastName = 'Test1'; con1.AccountId = Acc.Id; con1.Sum__c = 100; conList.add(con1); insert conList; delete con1; Test.StartTest(); TriggerHelperClass obj = new TriggerHelperClass(); update conList ; undelete con1; Test.StopTest(); }}
Hi Srikanth, Can you please try adding the following lines to your test class?
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@isTest
private class UpdatingAccountRelatedContactsCount_Test {
static Testmethod void accountRelatedContacts()
{
List<Contact> conList = new List<Contact>();
Account Acc = New Account();
Acc.Name = 'srikanth';
insert Acc;
Contact con = new Contact();
con.LastName = 'Test';
con.AccountId = Acc.Id;
con.Sum__c = 100;
conList.add(con);
Contact con1 = new Contact();
con.LastName = 'Test1';
con1.AccountId = Acc.Id;
con1.Sum__c = 100;
conList.add(con1);
insert conList;
delete con1;
Test.StartTest();
Map<Id,Contact> accMap = new Map<Id,Contact>();
for(Contact con : [Select id from Contact]){
accMap.add(con.id,con);
}
TriggerHelperClass obj = new TriggerHelperClass();
obj.deleteOperation(conList);
obj.updateOperation(conList, accMap);
update conList;
undelete con1;
Test.StopTest();
}
}