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

count number of opportunities on contact
Hi,
how to count number of opportunities related to contact,
total number of opportunities field on contact should be increment/decrement when opp is created/deleted.
how to solve it, pl help me with sample code.
u need to write a trigger on opp to get this on insert and delete events
could you help me with sample code..
On a high level code will like this
List<Account> accounts= [Select AccountId from Contact where id IN : Trigger.oldMap.keySet()];
if(null!=accounts && !accounts.isEmpty()){
List<AggregateResult> aggResult=[select count(id) Opp_count,AccountId from Opportunity where AccountId IN:accounts group by AccountId];
for (AggregateResult result : aggResult){
//d o your logic of updating Opps
}
}
The below trigger should help you :
Also, the trigger should run on update too because if you update the Account field on an Opportunity, it should add up to the Contact's field (new) and decrease the count on the Old Contact's field.
Let me know if this helps!
Thank you so much