3

I have a parent-and-child-object scenario. The parent will have a TOTAL field and the child a VALUE field. I want to show a total of all child VALUES on the parent.

I can either do that with a Master/Detail and use a roll-up summary field or a Lookup and a workflow or trigger to do the calculations I guess.

The reason I'm considering workflow / Apex trigger is that I may want to control the security of both objects independently. However, I realise that using workflow / trigger I will need to recalculate the total each time, meaning I have to grab all children, manually code the calculations, etc...

What are the pros / cons of both approaches?

3 Answers 3

5

The pros and cons list for rollup summary vs trigger is pretty short. In fact, you've taken care of most of the pros and cons of using a rollup summary in your question.

Rollup Summary:

Pros:

  • Standard behavior provided by Salesforce, well documented and understood
  • Doesn't require a trigger

Cons:

  • Requires a Master-Detail relationship
  • Has some restrictions on which fields can be aggregated (e.g. Can't roll up a formula field on the Detail object if it is a cross-object formula)
  • Limited to 25 rollup summary fields on a single object (prior to Winter '16, this limit was 10, and you could call Salesforce Support to get it increased)

Rollup via Trigger:

Pros:

  • Can work with lookup relationships (or a text field holding a parent record Id if you can't make a lookup to the object. OpportunityLineItem comes to mind)
  • Can work with more than 25 fields
  • Can work with a wider range of fields (provided you can write the summarization logic)

Cons:

  • Requires a trigger (and some test coverage)
  • Requires at least one query
  • Rollup must be explicitly coded

There are a few different ways to accomplish a rollup via trigger

  • Keep a map for each field you want to roll-up. Iterate over child records, and update the maps as you go
  • Use Aggregate functions in the SOQL query to do the rollup. [SELECT SUM(Field1__c) FROM myCustomObject__c]
  • Install Andrew Fawcett's Declarative Rollup Summary tool

Personally, when I need to use a trigger to do a rollup, I favor using a SOQL query to do the heavy lifting. Doing it via SOQL takes care of most of the null checks, and saves writing the same boilerplate summing logic over and over.

The declarative rollup summary tool is probably the best choice for most people outside of using a standard rollup summary field. I haven't used it myself, and don't know how well it would play with various trigger frameworks.

Conclusion:

In your case, the decision probably comes down to the question of how likely you are to need to manage the security of both objects independently.

If you will need to manage them independently, or think it's quite likely you will need to someday, go with a trigger rollup (looking at the declarative rollup summary tool first). Putting your two objects into a Master-Detail relationship would leave you with a technical debt that would be hard to resolve.

If their security can be managed together, and it doesn't look like that will ever change, go with the standard rollup via Master-Detail relationship.

1
  • Thanks, lots of useful info here!! Will experiment with some of these options. Commented Jun 7, 2016 at 12:32
2

If you are just looking for a summary field on the Parent Object, Roll-Up summary field is always a best bet. Security and accessibility can be managed via field-level security settings.

I would prefer in below sequence.
1. Roll-Up Summary
2. Process Builder (in case it is not achievable via option 1)
3. Workflows (in case it is not achievable via option 1 & 2)
4. Triggers (for more flexibility and to perform more actions than to determine the total).

As a thumb rule, configuration is always preferable than customization.

1

Always tend yourself to go with the Standard Salesforce functionality offerings than with code. Salesforce says "Why Re-invent a wheel again"! Use point-and-click than Code (ofcourse if the functionality you are looking for is already available).

When you have the roll-up summary calculations done automatically for you, don't take the extra efforts of time/energy to do the same thing by code. Coding for this scenario isn't straight forward too in the sense that you have to check for insert/update/delete/undelete conditions if you go for a trigger and not to forget all the apex coding limitations and making sure everything is bulkified.

Salesforce standard functionality takes care of all of this!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .