You need to sign in to do that
Don't have an account?
No Errors - Apex Trigger for Contact Count
The triggers below count the number of contacts and active contacts on an account. I am not getting any errors with the below triggers however, after doing some testing I found that recalculation is not taking place when a contact is deleted and field "Number of Contacts" is not updating properly. How can I correct this problem?

Thanks in advance!
trigger NumberOfContacts on Account (before insert, before update) { if(trigger.isinsert) for(account a:trigger.new) a.Number_of_contacts__c = 0; else { List<AggregateResult> agResult = [SELECT Count(ID) conCount, AccountId accId FROM Contact WHERE AccountId IN :trigger.new Group By AccountId]; for(AggregateResult result : agResult){ trigger.newmap.get((Id) result.get('accId')).Number_of_contacts__c = (Integer)result.get('conCount'); } for(Account act : Trigger.new){ act.Number_of_active_contacts__c = 0; } agResult = [SELECT Count(ID) conCount, AccountId accId FROM Contact WHERE AccountId IN :trigger.new AND Inactive__c = false Group By AccountId]; for(AggregateResult result : agResult){ trigger.newmap.get((Id) result.get('accId')).Number_of_active_contacts__c = (Integer)result.get('conCount'); } }
trigger NumberOfContactsOnAccount on Contact (after insert, after update, after delete, after undelete) { List<Contact> contacts = new list<contact>(); Map<Id,account> accounts = new map<id,account>(); if(trigger.new!=null) contacts.addAll(trigger.new); if(trigger.old!=null) contacts.addAll(trigger.old); for(contact c:contacts) accounts.put(c.accountid,new account(id=c.accountid)); accounts.remove(null); update accounts.values(); }
Thanks in advance!
Try this code. All I did was add this loop:
for (account a: trigger.new)
a.Number_of_contacts__c = 0;
So it will always set the number of contacts to 0, then update based on the agResult. If agResult is empty, it will stay as 0 and if it is not empty, it will be updated correctly.
All Answers
You need to query the accounts that the contacts look up to in your contact trigger. Try this code:
Hope this helps!
No errors, but still same thing when I delete a contact. 0 contacts in list view, however "Has Contacts?" field says yes and "Number of Contacts" field says 1.
So does your code work when you insert contacts under an account? It just doesn't work on delete?
NumberOfContacts: execution of BeforeUpdate caused by: System.QueryException: Aggregate query has too many rows for direct assignment, use FOR loop External entry point
To fix the issue above this code was created and does not update the count when a contact is deleted.
And this is the code for the contact object.
I hope this makes sense. I wish I could just have just one trigger to accomplsh this contact count.
Ok so I think I know what's going on here. When your account has just one contact and you delete that contact, your agResult will return you no rows, which means that it never goes in your for loop where you set the Number of Contacts field.
If you test using an account with 2 contacts, and you delete one of them, you should see that the trigger works fine. In order to resolve your issue, you need to check if agResult is empty, and if it is then set the Number of Contacts to 0. The only issue is how to bulkify your code...
Try this code. All I did was add this loop:
for (account a: trigger.new)
a.Number_of_contacts__c = 0;
So it will always set the number of contacts to 0, then update based on the agResult. If agResult is empty, it will stay as 0 and if it is not empty, it will be updated correctly.