function readOnly(count){ }
Don't have an account?
Search for an answer or ask a question of the zone or Customer Support.
You need to sign in to do that
Sign in to start searching questions
Signup for a Developer Edition
Sign in to start a discussion
trigger countContacts on Contact (after insert, after update, after delete) { list<contact> conList = new list<contact>(); list<account> accList = new list<account>(); set<ID> accIDs = new set<ID>(); if(trigger.isInsert || trigger.isUpdate) { for(contact con : trigger.new) { if(String.isNotBlank(con.accountId)) { accIds.add(con.accountId); } } } if(trigger.isDelete) { for(contact con : trigger.old) { accIDs.add(con.AccountId); } } if(accIDs.size()>0) { conList = [select name, id, accountId from contact where accountId IN:accIDs]; accList = [select name, id , number_of_contacts__c from account where ID IN:accIDs]; } for(account acc : accList) { acc.Number_of_contacts__c = conList.size(); } update accList; }
trigger NumberOfChild on Contact (After Insert,After Update,After Delete) { If(trigger.isAfter &&Trigger.isInsert){ NumberOfChildTriggerHandler.insertMethod(trigger.new); } If(trigger.isAfter &&Trigger.isUpdate){ NumberOfChildTriggerHandler.updateMethod(trigger.new,trigger.oldMap); } If(trigger.isAfter &&Trigger.isDelete){ NumberOfChildTriggerHandler.deleteMethod(trigger.old); } } ############################################################################################################# Handler Class public class NumberOfChildTriggerHandler { public static Void InsertMethod(list<Contact> lstCon ){ List<Account> accList=new List<Account>(); Set<Id> setAccIds = new Set<Id>(); for(Contact con : lstCon){ if(con.AccountId != null){ setAccIds.add(con.AccountId); } } for(Account acc :[Select id,Total_Contacts__c ,(Select id,name from contacts) from Account where Id in : setAccIds]){ acc.Total_Contacts__c = acc.contacts.size(); acclist.add(acc); } if(acclist.size()>0){ update accList; } } public static Void UpdateMethod(list<Contact> lstCon,map<Id,Contact>oldmap ){ List<Account> accList=new List<Account>(); Set<Id> setAccIds = new Set<Id>(); for(Contact con : lstCon){ if(con.AccountId != null && con.AccountId != oldMap.get(con.Id).AccountId){ setAccIds.add(con.AccountId); setAccIds.add(oldMap.get(con.Id).AccountId); } } for(Account acc :[Select id,Total_Contacts__c ,(Select id,name from contacts) from Account where Id in : setAccIds]){ acc.Total_Contacts__c = acc.contacts.size(); acclist.add(acc); } if(acclist.size()>0){ update accList; } } public static Void deleteMethod(list<Contact> lstCon){ List<Account> accList=new List<Account>(); Set<Id> setAccIds = new Set<Id>(); for(Contact con : lstCon){ if(con.AccountId != null){ setAccIds.add(con.AccountId); } } for(Account acc :[Select id,Total_Contacts__c ,(Select id,name from contacts) from Account where Id in : setAccIds]){ acc.Total_Contacts__c = acc.contacts.size(); acclist.add(acc); } if(acclist.size()>0){ update accList; } } }
Can you try the trigger logic on contact object.
If this solution helps, Please mark it as best answer.
Thanks,
@Sai Praveen need to write trigger using handler class
Try Below Class Please Mark It As Best Asnwer If It Helps
Thank You!