Salesforce Roll-Up Summary Fields on Lookup Relationships
A roll-up summary field, as the name suggests, aggregates data from related records. Different calculations are possible with roll-up fields. You can count the number of related records or find a maximum, minimum, or sum of a field in the related records.
- Use Roll-up summary fields for scenarios like
- calculating the total of all Open opportunities to show on the related Account record.
- Sum of the amount of all Order Line Items on the related Order record.
Roll-up Summary fields are more powerful than formula fields as unlike formula fields, they can perform specific calculations from multiple related records. Further, roll-up summary fields also support filtering criteria so one can choose to only aggregate Open opportunities or Order Line items that are yet to be delivered.
Roll-up summary fields are ONLY available on the Parent object when the parent and child objects are related in a Master-Detail relationship.
The summary fields are not available when records are related through the lookup relationship.
Since Salesforce isn’t planning to implement summary fields for the Lookup relationship anytime soon, this article looks at various ways in which to work around this limitation.
Don’t Reinvent the wheel - App from AppExchange
Perhaps, the obvious and straightforward method to alleviate the limitation is to use an App from the App Exchange. Salesforce is a very flexible platform and has lots of Trailblazers (or awesome developers) looking for issues for which they can provide a viable solution.
A simple search on the App exchange will bring up a lot of pre-existing applications which solve this specific issue.
- Out of the myriad choices available, the most recommended application (and also from personal experience) to extend the platform to support Roll-up summary fields are
- Declarative Roll-up for Lookups - Open Source
- Rollup Helper - Free
- Easy Rollup by Cyntexa - Free
- Roll-Up Summary Helper - Paid (240 USD per year)
Let it Flow, Let it flow …let it flow
No, we are not referring to the Toni Baxton song.
Salesforce Flow is another tool in our armory to implement the roll-up summary field feature. It is a “no-code” solution and would be preferred by Admins. An example and a deep dive best illustrate this.
Let's imagine a hypothetical scenario at Vinco Limited. Sales agents want to get in touch with Contacts related to the Accounts they manage. However, due to bad data practices and no duplicate management, they have lots of contacts with no email and/or contact number. Therefore, they are looking for a count of valid contacts to be displayed on a custom field on the Account record to ensure they don’t go looking for contacts where none exist.
Before we implement the flow, let us think about the implementation choices.
- A roll-up summary field should re-calculate the count of valid contacts in the following scenarios
- When a contact is created or updated.
- When a contact is deleted.
Further, the count of valid contacts should also be calculated for all existing records.
- Keeping the above in mind, the roll-up summary enhancement would be implemented as a set of the following flows:
- An Auto launched Flow to calculate valid contacts for a given account and update the parent Account. Account Id and an action (CREATE_UPDATE or Delete) would be passed as Input to this Flow.
- Two Record Triggered Flows. One Flow is to be invoked at Create and Update and the other for the Delete scenario. Each Flows invokes the auto-launched Flow as a sub-flow.
- A one-time scheduled Flow to update existing Accounts with the valid contact count.
Steps to Implement Roll-up Summary Field
Create the Auto-Launched Flow
Step 1: Create a Custom field Total Contacts on the Account object and assign it the necessary permissions.
Step 2: Create an Auto-Launched Flow (Setup | Flows | New Flow)
Step 3: Add a Get Record Element
Step 4: Create four Flow Variables
2. Action to store the action for which this subflow is invoked. Allowed values are (Create_Update and Delete)
3. AccountId to store the parent account Id on the changed contact
4. TotalValidContactsMinus1 to be used in Delete case
Step 5: Create an Assignment step
Start by setting the variable field as TotalValidContacts flow variable created in Step 4.
Then choose the Operator as “Equals Count”.
And finally, set it equal to the result from the “Get Records” element of Step 3.
Step 6: Next add a decision element to the flow
This is to ensure that the flow can be called for both Create/Update and delete scenarios.
Step 7: Add an Update Records action under the “If case is Create_Update” decision path”
Then, Add an Update Records action under the default outcome
The Flow, once completed, should look like the below image. Save and Activate the Flow.
Create the Create/Update Record Triggered Flow
Step 1: Create a Record Triggered Flow (Setup | Flows | New Flow) and configure it as per the image below
Step 2: Add a Sub Flow step and Include AccountId and Action to be sent to this subflow
Step 3: Save and activate the Flow.
Create the Delete Record Triggered Flow
Follow the steps for the Create Flow but choose the “A record is deleted” option when configuring the trigger for the flow. Save and activate the Flow.
Create the One Time Scheduled Flow
For brevity, the scheduled flow is not explained step by step here. However, the scheduled flow is configured to run once and get all accounts where the valid contact field is NULL.
It then does similar steps as the auto-launched flows to get all valid contacts and update the count variable on Account. Once finished, the flow should look like the below image
Activate all Flows and test the implementation.
Your no-code solution using flows is now ready.
The Apex way
The most complicated and code-heavy way to achieve the summary requirement is via Apex. More specifically, Triggers.
- Before we continue, let's recap the features Roll-up summary fields provide
- Aggregation of data. The aggregation is a calculated value based on either a SUM, MIN, or a MAX of related records
- A filtering criteria
- Let's recap the scenario we used in the Flow section of this article.
- Account is the Parent Object.
- Contact is the Child Object.
- Total_Contacts__c is the custom field created to store the rollup result.
- The filtering criteria is Emails on the contact should NOT be null.
A very basic code sample to implement this specific requirement is as follows
This is a very specific code example for our very specific requirement. As you can imagine, if you want multiple roll-up summary fields (to cater to different scenarios) on the Account object or roll-up summary fields on different objects, this approach will become cumbersome and difficult to manage.
A more generic and open source solution to the roll-up summary implementation is available here.
- This approach has the following advantages (reproduced verbatim from the link)
- Performs rollup on multiple such fields in a single aggregate soql query.
- Allows easy addition/removal of new fields to rollup as requirements changes over the span of time.
- Developer needs to write only a single trigger for multiple rollup fields.
- Allows developers to filter the child records getting rolled up, just like standard roll-up summary fields.
Let's summarise the options available for implementing the roll-up feature
Weighing up all pros and cons would be an essential step to decide on the solution to adopt for your specific use case.