I have created a custom Object called Application__c that has a lookup relationship to the Opportunity object. I have a field called total_applications__c on the Opportunity which aims to represent the total number of applications related to that opportunity.

I trying to populate that field though an apex trigger on the Application__c object that operates on after insert, after delete and after undelete events.

Considering the insert event :

My initial approach is that each time an application is being inserted I

-> create an oppset with all the opportunities that are being linked to from the applications that have been triggered

-> loop through all the applications that are link to an opportunity from the oppset

-> add the number of applications related to each opportunity in a map

-> loop though all the related opportunities from the applications that have currently being inserted

-> update that total_applications__c field with the computed values from the map.

However I feel that I can achieve the same functionality in fewer steps. Is there a better way of achieving what I described above?


You can use declarative-lookup-rollup-summaries for this purpose. You would then create a Lookup Rollup Summary something like:

  • Parent Object: Opportunity
  • Relationship Field: Opportunity__c
  • Child Object: Application__c
  • Field to Aggregate: Id
  • Aggregate Operation: Count
  • Aggregate Result Field: Total_Applications__c

Clicks not code!

|improve this answer|||||
  • thanks for your answer but I would prefer an inhouse solution rather than relying on a 3rd party implementation. And my example stated above is just a simplified version of the actual business application. – Marc Zaharescu Jun 15 '16 at 15:26
  • 1
    Rejecting all pre-built solutions out of hand seems somewhat obtuse. DLRS can certainly handle a lot more complexity than I have outlined here. – Adrian Larson Jun 15 '16 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.