You need to sign in to do that
Don't have an account?
PuranKishore
How to In account count the contacts with triggers
trigger ConCount on Account (After Update) {
for(Account a:trigger.new)
{
list<Contact> lscon = [Select id from Contact where accountid=:a.id];
update lscon;
}
}
You should collect the Account IDs first and then use them to query only once to get All the related contacts. Something like this below
Map<Id,Integer> acctIdMap = new Map<Id,Integer>();
for(Account a:Trigger.New) {
acctIdMap.put(a.Id,0);
}
List<Contact> conts = [Select Id,Name from Contact where AccountId IN :acctIdMap.keyValue() ORDER BY AccountID LIMIT 49999];
Integer i = 0;
while(i<conts.size() ) {
Integer x = 0;
x = acctIdMap.get(conts.get(i).AccountId)+1;
acctIdMap.put(acctIdMap.get(conts.get(i).AccountId),x);
i++;
}
Thanks
Utsav
[Do mark this answer as solution if it works for you and give a kudos.]