Salesforce trigger Count number of contacts on accounts using Trigger with best practices.
trigger CountTechBirdsTrigger on Contact (after insert, after update, after delete, after undelete) {
//call handler for best practice
if(Trigger.isinsert || Trigger.isupdate || trigger.isdelete || Trigger.isundelete){
CountTechBirdsApex.CountContactHelper(trigger.new, trigger.old);
}
}
public class CountTechBirdsApex {
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;
}
}
}
Option 2
Salesforce trigger that counts the number of contacts related to an account and updates a custom field (NoofContacts__c) on the Account object accordingly.
trigger CountTechBirdsTrigger on Contact (after insert, after update, after delete, after undelete) {
// Set to store Account IDs related to the contacts being modified
Set<Id> accIds = new Set<Id>();
// Collect Account IDs from the new contact records (after insert/update/undelete)
if (Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete) {
for (Contact c : Trigger.new) {
if (c.AccountId != null) {
accIds.add(c.AccountId);
}
}
}
// Collect Account IDs from the old contact records (before delete/update)
if (Trigger.isDelete || Trigger.isUpdate) {
for (Contact c : Trigger.old) {
if (c.AccountId != null) {
accIds.add(c.AccountId);
}
}
}
// Fetch all the Accounts that are associated with the collected Account IDs
if (!accIds.isEmpty()) {
List<Account> accList = [SELECT Id, NoofContacts__c, (SELECT Id FROM Contacts) FROM Account WHERE Id IN :accIds];
// Update the NoofContacts__c field with the count of related contacts
for (Account acc : accList) {
acc.NoofContacts__c = acc.Contacts.size();
}
// Update the Accounts in the database
update accList;
}
}
Explanation:
Trigger Events:
The trigger is set to run on after insert, after update, after delete, and after undelete events on the Contact object.
Handling Different Scenarios:
Insert/Update/Undelete: The trigger processes the new list of contact records.
Delete/Update: The trigger also processes the old list of contact records.
This ensures that the trigger accurately tracks changes when contacts are inserted, updated, deleted, or undeleted.
Fetching Related Accounts:
A SOQL query retrieves all related Account records based on the AccountId of the contacts being processed.
Updating the Contact Count:
The NoofContacts__c field on each account is updated to reflect the current number of related contacts.
Updating the Database:
Finally, the list of accounts with updated contact counts is saved back to the database using the update DML statement.
This code effectively counts the number of contacts related to each account and updates the custom field NoofContacts__c accordingly.
Comments