You need to sign in to do that
Don't have an account?
Dipthi
Plz correct my code - My code doesn't work when account is modified on contact
To show total no of contact records on Account
trigger ContactsTotalDisplayOnAccount on Contact (after insert, after update, after delete, after undelete) {
//Contact can be added/deleted/undeleted
List<contact> contacts = Trigger.isDelete ? Trigger.old : Trigger.new;
set<Id> accId = new set<Id>();
// Set is used to store unique values
for(contact con : contacts) {
If(con.AccountId != null) {
accId.add(con.AccountId);
}
}
List<account> acclist = new List<account>();
for(account acc : [Select Id,Name,Total_Number_of_Contacts__c,(Select Id from Contacts)
from Account
where Id IN :accId]) {
account acts = new Account();
acts.Id = acc.Id ;
acts.Total_Number_of_Contacts__c = acc.contacts.size();
acclist.add(acts);
}
update acclist;
}
trigger ContactsTotalDisplayOnAccount on Contact (after insert, after update, after delete, after undelete) {
//Contact can be added/deleted/undeleted
List<contact> contacts = Trigger.isDelete ? Trigger.old : Trigger.new;
set<Id> accId = new set<Id>();
// Set is used to store unique values
for(contact con : contacts) {
If(con.AccountId != null) {
accId.add(con.AccountId);
}
}
List<account> acclist = new List<account>();
for(account acc : [Select Id,Name,Total_Number_of_Contacts__c,(Select Id from Contacts)
from Account
where Id IN :accId]) {
account acts = new Account();
acts.Id = acc.Id ;
acts.Total_Number_of_Contacts__c = acc.contacts.size();
acclist.add(acts);
}
update acclist;
}
Based on these lines while in update trigger, your accId set is built from Trigger.New, which will only have new AccountIds.
List<contact> contacts = Trigger.isDelete ? Trigger.old : Trigger.new;
set<Id> accId = new set<Id>();
// Set is used to store unique values
for(contact con : contacts) {
If(con.AccountId != null) {
accId.add(con.AccountId);
}
}
While I liked the cleaner approach by CharuDutt, Delete and Insert will work without no problem, but update has the same problem.
if(trigger.isUpdate){
if(trigger.isAfter){
for(contact lstCon : trigger.new){
lst.add(lstcon.AccountId);
}
}
}
A quick fix would be to add another loop with trigger.old and add accountIds from that list as well to refresh the count on the old accounts.
if(trigger.isUpdate){
if(trigger.isAfter){
for(contact lstCon : trigger.new){
lst.add(lstcon.AccountId);
}
for(contact lstCon : trigger.old){
lst.add(lstcon.AccountId);
}
}
}
I would also add an additional check to see if accountId is updated, to limit your final update logic to only those record with accountId updates.
Here is the updated logic with only one loop and AccountId check.
if(trigger.isUpdate){
if(trigger.isAfter){
for(contact lstCon : trigger.new){
if(lstcon.AccountId != Trigger.oldMap.get(lstCon.Id).AccountId){
//AccountId Updated
//Add new AccountId to recalculate count
lst.add(lstcon.AccountId);
//Add old AccountId to recalculate count
lst.add(Trigger.oldMap.get(lstCon.Id).AccountId);
}
}
}
}
Note: I did not execute the code. So there might be some typo or syntax errors.
All Answers
Try the following code Please Mark it As Best Answer if It Helps
Thank you!
Thank u for ur response. Your code is all good when:
- contact is inserted/associated to account
- contact is deleted and undeleted
But, the problem I mentioned with my code stille exists. Ex: I have contact 'xyz' associated with 'account 'abc' -> I go to 'xyz' contact and change the account from 'xyz' to any other account -> 'Total child contacts' field shows no change on abc' account
Based on these lines while in update trigger, your accId set is built from Trigger.New, which will only have new AccountIds.
List<contact> contacts = Trigger.isDelete ? Trigger.old : Trigger.new;
set<Id> accId = new set<Id>();
// Set is used to store unique values
for(contact con : contacts) {
If(con.AccountId != null) {
accId.add(con.AccountId);
}
}
While I liked the cleaner approach by CharuDutt, Delete and Insert will work without no problem, but update has the same problem.
if(trigger.isUpdate){
if(trigger.isAfter){
for(contact lstCon : trigger.new){
lst.add(lstcon.AccountId);
}
}
}
A quick fix would be to add another loop with trigger.old and add accountIds from that list as well to refresh the count on the old accounts.
if(trigger.isUpdate){
if(trigger.isAfter){
for(contact lstCon : trigger.new){
lst.add(lstcon.AccountId);
}
for(contact lstCon : trigger.old){
lst.add(lstcon.AccountId);
}
}
}
I would also add an additional check to see if accountId is updated, to limit your final update logic to only those record with accountId updates.
Here is the updated logic with only one loop and AccountId check.
if(trigger.isUpdate){
if(trigger.isAfter){
for(contact lstCon : trigger.new){
if(lstcon.AccountId != Trigger.oldMap.get(lstCon.Id).AccountId){
//AccountId Updated
//Add new AccountId to recalculate count
lst.add(lstcon.AccountId);
//Add old AccountId to recalculate count
lst.add(Trigger.oldMap.get(lstCon.Id).AccountId);
}
}
}
}
Note: I did not execute the code. So there might be some typo or syntax errors.
Hi Dipthi,
Try this code
if you find this helpful mark it as the best answer.