0
public class countthecontacts {
    public void insertcontact(){
        list<Account> acclist = new list<Account>();
        list<contact> conlist = new list<contact>();
        set<id> accids = new set<id>();
        for(contact con : conlist){           
               accIds.add(con.accountId);           
        }
            for(account acc:[select id,No_of_Contacts__c,(select id from contacts) from account where id in:accids]){
            acc.No_of_Contacts__c=conlist.size();
             acclist.add(acc);   

    }
             update acclist;

        }
        }
2
  • 1
    Hi Keerthi. What did you try so far? Did you read any of the documentation on creating test classes yet? Commented Jun 28, 2019 at 7:51
  • 1
    Is there a reason you are not using a formula field on Account to do this for you instead of using code? This code is troublesome since it doesn't consider bulkified insert of multiple contacts either.
    – Phil W
    Commented Jun 28, 2019 at 8:52

0

Browse other questions tagged .