Activity Forums Salesforce® Discussions How can we count number of contacts associated with accounts? Reply To: How can we count number of contacts associated with accounts?

  • Raghav Chaturvedi

    Member
    July 13, 2017 at 1:10 pm

    trigger london1 on Contact (after insert,after delete) {

    list li=new list();
    if(trigger.isInsert)
    {
    for(contact c:trigger.new)
    {
    li.add(c.AccountId);
    }

    list con=[select name,(select id from contacts) from account where id in:li];
    for(account acc:con)
    {
    list c=acc.contacts;
    system.debug('account:'+acc.name+'Count_of_contact'+c.size());
    acc.count_of_contacts__c=c.size();
    update acc;
    }
    }
    }

    this will help you