+ Start a Discussion

Trigger instead of roll up

Roll ups are disabled in our org due to advanced currency.

Can a trigger be used to count opportunities associated to each contact?
more specifically, accounts and contacts associated to opportunities.  Not contact roles.
William TranWilliam Tran
Sure, you can use triggers to do many things, counting rollup can definitely be done.

where is best example apex?
Chris SweetChris Sweet
Yes, this can be done. You said "not contact roles", but that is exactly how Contacts are associated to Opportunities. That is, to get a rollup of Contacts associated with a given Opportunity, you'd basically just run a query like "Select count() from OpportunityContactRole Where OpportunityId = :<some_opportunity_id>". You can also use the OpportunityContactRole table to count the number of Opportunities any given Contact is part of.

Conversely, Opportunities are 1:1 with Accounts. Every Opportunity is associated with 1 and only 1 account, and accounts can have many opportunities. You could use a trigger on Opportunity insert/delete to update an # of Opportunities field on Account, if that's the question.

This is all assuming you're using out-of-the-box functionality.
with standard rollups, I can run an acounts & contacts report and include opportunity values per account.  Since I disabled rollups, I want to use  a trigger to rolup opportunities into accounts.  Then run a contacts & accounts report.  This has nothing to do with contact roles.

I hope this makes sense.
Chris SweetChris Sweet
Ah, yes, now I understand where the confusion around contacts is coming from. They really have no bearing in this discussion even though your original request said "opportunities associated to each contact" and "accounts and contacts associated to opportunities".

Simply put, yes, you can build a trigger that will do a roll-up of opportunity counts, amounts, etc. reletively easy on accounts.
is there a sample apex code my developer can start using? 
Kiran  KurellaKiran Kurella

Check out Andy's blog for a Rollup Summary App:

Hi Samantha,

Try to manipulate the code as per your need just replace API names of your object and fields accordingly .

http://manojjena20.blogspot.in/2015/09/roll-up-summery-trigger.htm (http://manojjena20.blogspot.in/2015/09/roll-up-summery-trigger.html)