+ Start a Discussion
bhargavi k 16bhargavi k 16 

Total no of contacts displayed in account level using map

I didnt get the result of this...can u anyone suggest me...whrere i done a mistake...and i getting error msg like:Method does not exist or incorrect signature: [Contact].size()
Trigger code:
trigger totalcontrigger on Contact (after insert, after update) 
{
    if(trigger.isinsert && trigger.isafter)
    {
        contacthelp.totalcontacts(trigger.new);
    }
}
Class Code:
public class contacthelp {
    public static void totalcontacts(list<contact> conlist){
        Map<id,contact> conmap=new map<id,contact>();
        for(contact c:conlist)
        {
           conmap.put(c.accountid, c) ;
        }
        list<account> acclist=[select id, totalcontacts__c from account where id in:conmap.keyset()];
        for(account a:acclist)
        {
            a.totalcontacts__c=conmap.get(a.id).size();
        }
        update acclist;        
    }
}
RAM AnisettiRAM Anisetti
Hi try this one
 
trigger UpdateOrder on Contact (after insert, after update, after delete, after undelete) {

   List<Account> ct = new List<Account>();
   
   Set<Id> custord = new Set<Id>();
   
   if(Trigger.isDelete) {
     for(Contact test:Trigger.Old) {
      
        custord.add(test.Accountid);   
    
     }   
   
   }
   else
   if(Trigger.isUpdate) {

     for(Contact test:Trigger.New) {
      
        custord.add(test.Accountid);   
    
     }

     for(Contact test:Trigger.Old) {
      
        custord.add(test.Accountid);   
    
     }   
   
   }
   else
   {
     for(Contact test:Trigger.New) {
      
        custord.add(test.Accountid);   
    
     }
   }
   
   AggregateResult[] groupedResults = [SELECT COUNT(Id), Accountid FROM Contact where Accountid IN :custord GROUP BY Accountid ];
   
   for(AggregateResult ar:groupedResults) {
     
     Id custid = (ID)ar.get('Accountid');
     
     Integer count = (INTEGER)ar.get('expr0');
     
     Account cust1 = new Account(Id=custid);
     
     cust1.totalcontacts__c = count;
     
     ct.add(cust1);
      
   }
   
   
   update ct;

}

 
rohan deoskarrohan deoskar
Hello  bhargavi,
Actually the problem is Here,
 a.totalcontacts__c=conmap.get(a.id).size(); 
here You are trying to get the size() of single contact (because your map is <id,Contact>)which is syntatically wrong.
 that is why it is throwing the error. you can do it something like this,

for(Account acc :[SELECT id,totalcontacts__c,Contacts FROM Account WHERE id IN: conmap.keySet()]){
acc.totalcontacts__c= acc.Contacts.size();
}
hope it works for You :)
rohan deoskarrohan deoskar
If you are trying to get the number of Contacts which are inserted or updated (Because of which the trigger is Fired) in The Account. You can do something like This,
 Map<id,Integer> conmap=new map<id,Integer>();
        for(contact c:conlist)
        {
          if( ! conmap.containsKey(c.accountid)){
           conmap.put(c.accountid, 0) ;
            }
       else{
           conmap.get(c.accountid).put( conmap.get(c.accountid) + 1);
              }
        }

 list<account> acclist=[select id, totalcontacts__c from account where id in:conmap.keyset()];
        for(account a:acclist)
        {
            a.totalcontacts__c=conmap.get(a.id);
        }
        update acclist;