You need to sign in to do that
Don't have an account?

To show the contact count details in account details page
In account page,the contact is created ,I want to show the number of count of contacts records created that is associated with that account.I have tried to write the trigger for that.But some error getting in that trigger.Please rectify to solve that bugs.
trigger NoofCountContacts on Account (after insert,after update) {
List <Account> con = new List <Account>();
for (contact con1:Trigger.new){
String Id= Apexpages.currentPage().getParameters().get('Account.Id');
Integer sum =[SELECT COUNT() FROM contact where accid =:Trigger.old.keyset(Id)];
con.Number_Of_Contacts= sum;
}
update con;
}
trigger NoofCountContacts on Account (after insert,after update) {
List <Account> con = new List <Account>();
for (contact con1:Trigger.new){
String Id= Apexpages.currentPage().getParameters().get('Account.Id');
Integer sum =[SELECT COUNT() FROM contact where accid =:Trigger.old.keyset(Id)];
con.Number_Of_Contacts= sum;
}
update con;
}
or if the records have lookup relationship we can write a trigger .
trigger countofcontact on Account (after insert,before update) {
list<id> l1=new list<id>();
list<account> listacc=new list<account>();
list<contact> listcon=new list<contact>();
map<id,integer> m1=new map<id,integer>();
for(account acc:trigger.new)
{
l1.add(acc.id);
}
for(account a1:trigger.old)
{
l1.add(a1.id);
}
list<account> l=[select id,name from account where id in:l1];
list<contact> lc=[select id,name from contact where id in:l1];
for(account a:trigger.new)
{
listcon.clear();
for(contact con:lc)
{
listcon.add(con);
m1.put(con.accountid,listcon.size());
}
}
if(listcon.size()>0)
{
for(account aa:l)
{
if(m1.get(aa.id)==null)
{
aa.count_of_contact__c=0;
}
else
{
aa.count_of_contact__c=m1.get(aa.id);
l.add(aa);
}
}
}
if(l.size()>0)
{
update l;
}}