function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
suri.sfdcsuri.sfdc 

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.

 

kiranmutturukiranmutturu

u need to write a trigger on opp to get this on insert  and delete events 

suri.sfdcsuri.sfdc

could you help me with sample code..

abhishektandon2abhishektandon2

 

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
}
}

vishal@forcevishal@force

The below trigger should help you :

 

trigger countNumberOfOpp on Opportunity (after delete, after insert, after update) 
{
	set<Id> setAccountIds = new set<Id>();
	map<Id, Integer> mapAccountToOpp = new map<Id, Integer>();
	List<Contact> lstContactUpdate = new List<Contact>();
	
	if(trigger.isInsert || trigger.isUpdate)
	{
		for(Opportunity o : trigger.new)
		{
			setAccountIds.add(o.AccountId); // take all the related Account Id's in a set from trigger.new in case of insert/update
		}
	}
	else
	{
		for(Opportunity o : trigger.old)
		{
			setAccountIds.add(o.AccountId); // take all the related Account Id's in a set from trigger.old in case of delete
		}
	}
	
	for(Account a : [Select Id, (Select Id From Opportunities) From Account Where Id In :setAccountIds])
	{
		mapAccountToOpp.put(a.Id, a.Opportunities.size()); // add each account id with the number of opportunities related to it
	}
	
	for(Contact c : [Select Id, AccountId, Number_of_Opportunities__c  From Contact Where AccountId In :mapAccountToOpp.keySet()])
	{
		c.Number_of_Opportunities__c  = mapAccountToOpp.get(c.AccountId);  // from the contact, we can get the number of opp related to it's parent account
		lstContactUpdate.add(c);
	}
	
	if(lstContactUpdate.size() > 0)
		update lstContactUpdate; // update the contacts
}

 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!

suri.sfdcsuri.sfdc

Thank you so much

Betsy Landon 5Betsy Landon 5
Wow! I think I'll let Marketo do the heavy lifting here and map the fields to Salesforce. I will create triggered campaigns and three Marketo Score fields incrementing +1 for each: # Won Opps (triggered by "Is Won"), # Lost Opps (triggered by "Reason"), # Open Opps (triggered by Contact is added to an opportunity and filter by any open stage). We decided not to mix scoring for opportunity association with demographic or behavior scores as it would skew it in a way that makes these scores less meaningful. Knowing the quantity of open, won or lost opps in separate fields is better for us.