//if you use Trigger.new below in place of Trigger.old you will end up with //System.NullPointerException:Attempt to de-reference a null object for(Contact con : Trigger.old) { accountIds.add(con.AccountId); } } List<Account> listAccs = [Select id,name,No_of_contacts__c ,(Select id from contacts) from Account where Id in : accountIds]; for(Account acc :listAccs) { acc.No_of_contacts__c= acc.contacts.size(); } update listAccs; }
You can use the below code as apex Trigger on Contact.
trigger CountContactOnAccount on Contact (after INSERT, after UPDATE, after DELETE ) {
Set <Id> accountIds = new Set <Id>();
List <Account> lstAccountsToUpdate = new List <Account>();
if(Trigger.isInsert){
for(Contact con:trigger.new){
accountIds.add(con.accountID);
}
}
if(Trigger.isUpdate|| Trigger.isDelete){
for(Contact con:trigger.old){
accountIds.add(con.accountID);
}
}
for(Account acc:[SELECT Id,Name,Count_Contact__c,(Select Id from Contacts) from Account where Id IN: accountIds]){
Account accObj = new Account ();
accObj.Id = acc.Id;
accObj.Count_Contact__c = acc.Contacts.size();
lstAccountsToUpdate.add(accObj);
}
UPDATE lstAccountsToUpdate;
}
If this solution helps, Please mark it as best answer.
Try to below code: trigger CountContactOnAccount on Contact (after INSERT, after UPDATE, after DELETE ) { Set <Id> accountIds = new Set <Id>(); List <Account> lstAccountsToUpdate = new List <Account>(); if(Trigger.isInsert){ for(Contact con:trigger.new){ accountIds.add(con.accountID); } } if(Trigger.isUpdate|| Trigger.isDelete){ for(Contact con:trigger.old){ accountIds.add(con.accountID); } } List<Account>accList= [SELECT Id,Name,Count_Contact__c,(Select Id from Contacts) from Account where Id IN: accountIds]; for(Account acc:accList){ acc.Count_Contact__c = acc.Contacts.size(); lstAccountsToUpdate.add(acc); }
trigger countchildrec on Contact (After insert,After Delete,After Undelete) {
set<id>accountIds=new set<id>();
if(trigger.isinsert || trigger.isUndelete){
for(contact c:trigger.new){
accountIds.add(c.AccountId);
}
}
if(Trigger.isDelete){
//if you use Trigger.new below in place of Trigger.old you will end up with
//System.NullPointerException:Attempt to de-reference a null object
for(Contact con : Trigger.old)
{
accountIds.add(con.AccountId);
}
}
List<Account> listAccs = [Select id,name,No_of_contacts__c ,(Select id from contacts) from Account where Id in : accountIds];
for(Account acc :listAccs)
{
acc.No_of_contacts__c= acc.contacts.size();
}
update listAccs;
}
if it helps please mark as best answer...
Thanks
You can use the below code as apex Trigger on Contact.
If this solution helps, Please mark it as best answer.
Thanks,
Please try with below code will work for undelete also.
If this helps, please mark it as best answer.
Regards,
Ankaiah bandi
Try Below Code Please Mark It As Best Answer If It Helps
Thank You!
trigger CountContactOnAccount on Contact (after INSERT, after UPDATE, after DELETE ) {
Set <Id> accountIds = new Set <Id>();
List <Account> lstAccountsToUpdate = new List <Account>();
if(Trigger.isInsert){
for(Contact con:trigger.new){
accountIds.add(con.accountID);
}
}
if(Trigger.isUpdate|| Trigger.isDelete){
for(Contact con:trigger.old){
accountIds.add(con.accountID);
}
}
List<Account>accList= [SELECT Id,Name,Count_Contact__c,(Select Id from Contacts) from Account where Id IN: accountIds];
for(Account acc:accList){
acc.Count_Contact__c = acc.Contacts.size();
lstAccountsToUpdate.add(acc);
}
UPDATE lstAccountsToUpdate;