You need to sign in to do that
Don't have an account?
Naveen Velkur 6
How to write test class for the below trigger?
trigger CountContacts on Contact (after insert, after delete) {
List<Account> addAccCount= new List<Account>();
Set<Id> addConId= new Set<Id>();
if(trigger.isInsert){
for(Contact con:trigger.new){
addConId.add(con.AccountId);
}
}
if(trigger.isDelete){
for(Contact con:trigger.old){
addConId.add(con.AccountId);
}
}
List<Account> accList=[SELECT Id,CountofContacts__c, (SELECT Id FROM Contacts) FROM Account WHERE Id in: addConId];
for(Account acc:accList){
acc.CountofContacts__c = acc.Contacts.size();
addAccCount.add(acc);
}
Update addAccCount;
}
if(trigger.isDelete){
for(Contact con:trigger.old){
addConId.add(con.AccountId);
}
}
List<Account> accList=[SELECT Id,CountofContacts__c, (SELECT Id FROM Contacts) FROM Account WHERE Id in: addConId];
for(Account acc:accList){
acc.CountofContacts__c = acc.Contacts.size();
addAccCount.add(acc);
}
Update addAccCount;
}
List<Account> addAccCount= new List<Account>();
Set<Id> addConId= new Set<Id>();
if(trigger.isInsert){
for(Contact con:trigger.new){
addConId.add(con.AccountId);
}
}
if(trigger.isDelete){
for(Contact con:trigger.old){
addConId.add(con.AccountId);
}
}
List<Account> accList=[SELECT Id,CountofContacts__c, (SELECT Id FROM Contacts) FROM Account WHERE Id in: addConId];
for(Account acc:accList){
acc.CountofContacts__c = acc.Contacts.size();
addAccCount.add(acc);
}
Update addAccCount;
}
if(trigger.isDelete){
for(Contact con:trigger.old){
addConId.add(con.AccountId);
}
}
List<Account> accList=[SELECT Id,CountofContacts__c, (SELECT Id FROM Contacts) FROM Account WHERE Id in: addConId];
for(Account acc:accList){
acc.CountofContacts__c = acc.Contacts.size();
addAccCount.add(acc);
}
Update addAccCount;
}
If you find this answer helpful, Please mark it as the best answer.
All Answers
If you find this answer helpful, Please mark it as the best answer.
1. Trigger Class 2. Handler Class
3. Test Class Hope this will help.
Thanks!