You need to sign in to do that
Don't have an account?
bhargavi 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;
}
}
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;
}
}
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 :)
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;