function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
manjunath vivekmanjunath vivek 

​I am trying to count the number of contacts associated with the account.Code is saved , but not works.Please help me.

I am trying to count the number of contacts associated with the account.Code is saved , but  not works.Please help me.

Trigger counting on contact(after update,after insert){
List<account> accids=New list<Account>();
For(contact con:trigger.new){
Accids.add(con.account);
}
For(contact con:trigger.new)
{
Account Acc;
Acc.count__c=accids.size();
}
}
Best Answer chosen by manjunath vivek
Sandeep M 1Sandeep M 1
@manjunath try like this
trigger countcontacts on Contact (after insert, after delete) {
    Set<Id> aId = new Set<Id>();
    
    if(Trigger.isInsert){
        System.debug('Insert contact for trigger.new - '+Trigger.New);
        for(Contact opp : Trigger.New){
            aId.add(opp.AccountId);
        }
        List<Account> acc = [select id,count_of_contacts__c from Account where Id in:aId];
        List<Contact> con = [select id from contact where AccountId in :aId];
        
        for(Account a : acc){
            a.count_of_contacts__c=con.size();
        }
        update acc;
        System.debug('Number is '+acc);
    }
    
    if(Trigger.isDelete){
        System.debug('delete contact for trigger.old - '+Trigger.Old);
        for(Contact opp : Trigger.old){
            aId.add(opp.AccountId);
        }
        List<Account> acc = [select id,count_of_contacts__c from Account where Id in:aId];
        List<Contact> con = [select id from contact where AccountId in :aId];
        
        for(Account a : acc){
            a.count_of_contacts__c=con.size();
            
        }
        update acc;
        System.debug('Number is '+acc);
    }

All Answers

RishavRishav
Hii Manjunath,
                you can try this code it will give you the number of contact as well as id of all the contact 
trigger ContactCount on Contact (After insert,After update) {
  // list<contact> con = new list<contact>();
    set<ID>AccountId = new set<ID>();
    for(contact c:trigger.new)
    {
        AccountId.add(c.AccountId);
    }
    // Getting id of all contact whose account id is the same as current record account id 
    List<Contact> con = [select id  from contact where AccountID IN:AccountId];
    system.debug('size of Contact is :'+con.size());
    
    // For printing all the contact id that belong with current account
    for(contact co:con)
    {
     system.debug('Contact id is'+co.id);
    }
    

}
If your problem solved then please mark as best answer or ask your doubt.

Thanks 
Rishav
Frédéric TrébuchetFrédéric Trébuchet
Hello,

Why don't you use a roll-up summary field for this purpose?
Else, in the 2nd part of the trigger, you should update the Account record in the database, not just the memory structure (I think so).

Hope this helps,
Fred
lkatneylkatney
Hey,

We cannot create rollup summary for contacts on accounts. We don't get option for that.

With trigger, we can simplye achieve this with a field on account and a contact's trigger will update the field. Trigger should count values on insert , updated as well as delete.

Hope this helps!
Frédéric TrébuchetFrédéric Trébuchet
OK, I didn't have this restriction in mind.
However, you have to explicitly update the Account at the end.
Have a look at this link https://success.salesforce.com/answers?id=90630000000h3mNAAQ
manjunath vivekmanjunath vivek
Hi Rishav, 

Your code helps me a lot, but one more thing, I want to assign the size to custom field Count__C, can you please let me know, how to do that.
manjunath vivekmanjunath vivek
I have modified the code as below, still i am getting the error message.
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger counting caused an unexpected exception, contact your administrator: counting: execution of AfterInsert caused by: System.QueryException: List has more than 1 row for assignment to SObject: Trigger.counting: line 7, column 1


Trigger counting on contact(after update,after insert){
Set<account> accids=New Set<Account>();
For(contact c:trigger.new){
Accids.add(c.Account);
}
List<Contact> con = [select id  from contact where AccountID IN:accids];
List<Account> Acc=[Select count__C from account where id IN:accids ];
For(contact c:trigger.new)
{
Acc.count__c=con.size();
}
}
Sandeep M 1Sandeep M 1
@manjunath try like this
trigger countcontacts on Contact (after insert, after delete) {
    Set<Id> aId = new Set<Id>();
    
    if(Trigger.isInsert){
        System.debug('Insert contact for trigger.new - '+Trigger.New);
        for(Contact opp : Trigger.New){
            aId.add(opp.AccountId);
        }
        List<Account> acc = [select id,count_of_contacts__c from Account where Id in:aId];
        List<Contact> con = [select id from contact where AccountId in :aId];
        
        for(Account a : acc){
            a.count_of_contacts__c=con.size();
        }
        update acc;
        System.debug('Number is '+acc);
    }
    
    if(Trigger.isDelete){
        System.debug('delete contact for trigger.old - '+Trigger.Old);
        for(Contact opp : Trigger.old){
            aId.add(opp.AccountId);
        }
        List<Account> acc = [select id,count_of_contacts__c from Account where Id in:aId];
        List<Contact> con = [select id from contact where AccountId in :aId];
        
        for(Account a : acc){
            a.count_of_contacts__c=con.size();
            
        }
        update acc;
        System.debug('Number is '+acc);
    }
This was selected as the best answer
manjunath vivekmanjunath vivek
Thank you Sndeep, it works perfectly.