Create field called “Count of Contacts” on Account Object. When we add the Contacts for that Account then count will populate in the field on Account details page. When we delete the Contacts for that Account, then Count will update automatically.
Hi Please Follow This Step By Step To Resolve the Issue.
Step 1: Create TriggerHandler Class
public virtual class TriggerHandler
{
public virtual void beforeInsert() {}
public virtual void afterInsert() {}
public virtual void beforeUpdate() {}
public virtual void afterUpdate() {}
public virtual void beforeDelete() {}
public virtual void afterDelete() {}
public virtual void afterUnDelete() {}
public void processTrigger() {
if (Trigger.isBefore) {
//entered before event
if (Trigger.isInsert) {
beforeInsert();
} else if (Trigger.isUpdate) {
beforeUpdate();
} else if (Trigger.isDelete) {
beforeDelete();
}
} else {
//entered after event
if (Trigger.isInsert) {
afterInsert();
} else if (Trigger.isUpdate) {
afterUpdate();
} else if (Trigger.isDelete) {
afterDelete();
} else if (Trigger.isUndelete) {
afterUnDelete();
}
}
}
}
Step 2: Create ContactTriggerHandler Class That Extends TriggerHandler Class - The Solution Follows the Factory Design Pattern and All the Logic was Implemented in Class Not in Trigger.
public class ContactTriggerHandler extends TriggerHandler {
//Method: afterInsert
//Usage: this method is invoked in the context of "Trigger.isAfter && Trigger.isInsert" , so all the logic w.r.t after insert trigger context will be implemented here.
public override void afterInsert() {
list<Contact> triggerNew = Trigger.New;
set<ID> accountIdSet = new set<ID>();
for (Contact newContact: triggerNew) {
//check if contct is related to Account or not
if (newContact.AccountId != null) {
//Yes, Contact is realted to Account. Then add accountId to accountIdSet.
accountIdSet.add(newContact.AccountId);
}
}
if (!accountIdSet.isEmpty()) {
calculateCountOnAccount(accountIdSet);
}
}
//Method: afterUpdate
//Usage: this method is invoked in the context of "Trigger.isAfter && Trigger.isUpdate" , so all the logic w.r.t after update trigger context will be implemented here.
public override void afterUpdate() {
list<Contact> triggerNew = Trigger.New;
Map<Id, Contact> triggerOldMap = (Map<Id, Contact>) Trigger.oldMap;
set<ID> accountIdSet = new set<ID>();
for (Contact newContact: triggerNew) {
Contact oldContact = triggerOldMap.get(newContact.Id);
//check contact account is changed or not and Contact is realted to Account.
if (newContact.AccountId != null) {
if (newContact.AccountId != oldContact.AccountId) {
//Yes, Contact is realted to Account. Then add accountId to accountIdSet.
accountIdSet.add(newContact.AccountId);
}
} else if (newContact.AccountId == null && oldContact.AccountId != null) {
accountIdSet.add(oldContact.AccountId);
}
}
if (!accountIdSet.isEmpty()) {
calculateCountOnAccount(accountIdSet);
}
}
//Method: afterDelete
//Usage: this method is invoked in the context of "Trigger.isAfter && Trigger.isdelete" , so all the logic w.r.t after delete trigger context will be implemented here.
public override void afterDelete() {
list<Contact> triggerOld = Trigger.Old;
set<ID> accountIdSet = new set<ID>();
for (Contact oldContact: triggerOld) {
if (oldContact.AccountId != null) {
//Yes, Contact is realted to Account. Then add accountId to accountIdSet.
accountIdSet.add(oldContact.AccountId);
}
}
if (!accountIdSet.isEmpty()) {
calculateCountOnAccount(accountIdSet);
}
}
//Method: afterUnDelete
//Usage: this method is invoked in the context of "Trigger.isAfter && Trigger.isUndelete" , so all the logic w.r.t after undelete trigger context will be implemented here.
public override void afterUnDelete() {
list<Contact> triggerNew = Trigger.New;
Map<Id, Contact> triggerOldMap = (Map<Id, Contact>) Trigger.oldMap;
set<ID> accountIdSet = new set<ID>();
for (Contact newContact: triggerNew) {
if (newContact.AccountId != null) {
//Yes, Contact is realted to Account. Then add accountId to accountIdSet.
accountIdSet.add(newContact.AccountId);
}
}
if (!accountIdSet.isEmpty()) {
calculateCountOnAccount(accountIdSet);
}
}
//method will calculate count of all child contacts w.r.t each Account
private void calculateCountOnAccount(set<ID> accountIdSet) {
list<Account> updateAccountList = new list<Account>();
//Parent to child query : Getting Accounts and their related Contacts
for (Account account: [Select Id, Number_of_contacts__c, (SELECT Id from Contacts) FROM Account WHERE ID in: accountIdSet]) {
Integer contactCount = 0;
for (Contact contact: account.Contacts) {
contactCount++;
}
account.Number_of_Contacts__c = contactCount;
updateAccountList.add(account);
}
if (!updateAccountList.isEmpty()) {
update updateAccountList;
}
}
}
Step 3: Implement TriggerHandlerFactory Class
public class TriggerHandlerFactory {
public static TriggerHandler createTriggerHandler(Schema.SObjectType sObjType) {
TriggerHandler handler;
if (sObjType == Contact.sObjectType) {
handler = new ContactTriggerHandler();
}
if (handler == null) {
throw new TriggerException('No Trigger Handler registered for Object Type: ' + sObjType);
}
return handler;
}
public class TriggerException extends Exception {}
}
Step 4: Finally Implement Triiger
trigger ContactTrigger on Contact(after delete, after insert, after undelete, after update, before delete, before insert, before update) {
TriggerHandler handler = TriggerHandlerFactory.createTriggerHandler(Contact.sObjectType);
handler.processTrigger();
}
Please Mark It as a Best Solution, If you find the Solution for your Problem. Thanks & Regards, Sharan Kumar
https://success.salesforce.com/answers?id=90630000000h3mNAAQ
If this answer helps you, please mark it as accepted.
Regards,
Tushar Sharma
https://newstechnologystuff.com/
All Answers
https://success.salesforce.com/answers?id=90630000000h3mNAAQ
If this answer helps you, please mark it as accepted.
Regards,
Tushar Sharma
https://newstechnologystuff.com/
Step 1: Create TriggerHandler Class Step 2: Create ContactTriggerHandler Class That Extends TriggerHandler Class - The Solution Follows the Factory Design Pattern and All the Logic was Implemented in Class Not in Trigger. Step 3: Implement TriggerHandlerFactory Class Step 4: Finally Implement Triiger
Please Mark It as a Best Solution, If you find the Solution for your Problem.
Thanks & Regards,
Sharan Kumar