You need to sign in to do that
Don't have an account?
Count number of Contacts for Particular Account
Take a number field on Account. Whnever a Contact is inserted for a particular account, number value in accoutn ahould be increamented by 1.
You need to sign in to do that
Don't have an account?
Take a number field on Account. Whnever a Contact is inserted for a particular account, number value in accoutn ahould be increamented by 1.
You could probably use a cross object workflow to do this
Hi,
Try this code......
List<contact> conforaccounts = [select id,Accountid from contact];
List<Account> Noofcontacts = [select id,No_of_Contacts__c from Account];
for (Account acct : Noofcontacts) {
list<ID> conIds = new list<ID>();
for (Contact con : conforaccounts ) {
if (con.AccountId == acct.Id)
conIds.add(con.Id);
acct.No_of_Contacts__c = conIds.size();
}
}
update Noofcontacts;
}
First trigger calls the parent records for updating. The second performs a summed query. This makes sure that contacts do not "slip through the cracks" and are always counted correctly.
still no body give best solution or at least hint to solve.
List<Account> acc=new List<Account>();
List<Contact> contTemp =new List<Contact>();
contTemp.lastname = acc.name;
contTemp.accountid = acc.Id;
insert c;
for(account a: acc)
{
integer i=0;
List<Contact> contTemp =new List<Contact>();
if([select count() from contact where contTemp.accountid=:a.id]>0){
a.No_of_contacts__c=i;
i++;}
}
}
}
}
i tried like this am new lerner. somuch mistakes. and atleast give a hint which concepts cover in this assignment.
Plenty of answers were given, but none seemed to satisfy your request, so I'm afraid I have no idea what you're asking. Your code makes no sense whatsoever, and your objective is unclear. What are you hoping to achieve, besides the generic description you provided in your original post?
Thanks for posting the code for calculating the count.
I was trying to figure out in your code, why we need Account trigger to update count. Can't we do it through the Contact Trigger only thus saving a SOQL?
Thanks