Sometimes we have Requirement to have a number of Counts of Contact on Account, just like Rollup Field Salesforce. But Salesforce has provided Rollup Summary Field on the Master-Detail Object. Below is Sample code for the Same.
Apex Trigger :
trigger trgContactTrigger on Contact (before insert, after insert, before update,after update,after Delete)
{
ContactTriggerHandler objClass = new ContactTriggerHandler();
objClass.runTrigger();
}
Apex Class:
Public class ContactTriggerHandler{
public void runTrigger() {
// Method will be called to handle Before Insert events
if (Trigger.isBefore && Trigger.isInsert) {
onBeforeInsert((List < Contact> ) trigger.new, null);
}
// Method will be called to handle Before Update events
if (Trigger.isBefore && Trigger.isUpdate) {
onBeforeUpdate((List < Contact> ) trigger.new, (Map < Id, Contact> ) trigger.OldMap);
}
if (Trigger.isAfter && Trigger.isInsert) {
onAfterInsert((List < Contact> ) trigger.new, null);
}
if (Trigger.isAfter && Trigger.isUpdate) {
onAfterUpdate((List < Contact> ) trigger.new, null);
}
if (Trigger.isAfter && Trigger.isDelete) {
onAfterDelete((List < Contact> ) trigger.OldMap.values(), null);
}
}
// Method calls all the methods required to be executed before insert
public void onBeforeInsert(List < Contact> lstTriggerNew, Map < Id, Contact> mapTriggerOld) {
}
// Method calls all the methods required to be executed before insert
public void onAfterUpdate(List < Contact> lstTriggerNew, Map < Id, Contact> mapTriggerOld) {
}
// Method calls all the methods required to be executed before insert
public void onAfterInsert(List < Contact> lstTriggerNew, Map < Id, Contact> mapTriggerOld) {
updateContactCountThroughTrigger(lstTriggerNew, mapTriggerOld);
}
// Method calls all the methods required to be executed after delete
public void onAfterDelete(List < Contact> lstTriggerNew, Map < Id, Contact> mapTriggerOld) {
updateContactCountThroughTrigger(lstTriggerNew, mapTriggerOld);
}
// Method calls all the methods required to be executed before update
public void onBeforeUpdate(List < Contact> lstTriggerNew, Map < Id, Contact> mapTriggerOld) {
}
public void updateContactCountThroughTrigger(List < Contact> lstTriggerNew, Map < Id, Contact> mapTriggerOld){
Set< Id > setOfAccountID = new Set< Id >();
if(lstTriggerNew!= null){
For(Contact objCon : lstTriggerNew){
if(objCon.AccountId != null)
setOfAccountID.add(objCon.AccountId);
}
if(setOfAccountID == null || setOfAccountID.isEmpty())
return;
Integer ContactCount ;
List< Account > lstofAccount = new List< Account >();
For (Account objAcc :[Select id , (Select id,AccountId FROM Contacts), No_of_Contact_Trigger__c FROM Account WHERE ID IN : setOfAccountID]){
ContactCount= 0;
For (Contact objContact : objAcc.Contacts){
if(objContact.AccountId != null )
ContactCount++;
}
objAcc.No_of_Contact_Trigger__c =ContactCount;
lstofAccount.add(objAcc);
}
if(!lstofAccount.IsEmpty())
update lstofAccount;
}
}
}
This Code is Tested in my personal Dev org. Please let me know if you have any suggestions for this.