Creating a Roll-Up Summary with Flow

Creating a Roll-up Summary With Flow

Roll-up summary fields calculate values from related records, such as those in a related list, and display the value in the parent record. It is possible to perform different types of calculations with a roll-up summary field. You can count the detail records related to the parent record. Or, you can calculate the sum, minimum value, or maximum value of a field in the detail records. However, there is an important rule. The detail record must be related to the master through a master-detail relationship. It means that if the child record/object is related to the parent with a lookup field, you cannot create a roll-up summary field. But still, there are some exceptions. For example, it is possible to create a roll-up summary field on the account object using the values of related opportunities.

To learn more about roll-up summary fields, you can read this article. If you have any questions regarding the relationship types, I recommend you to complete this module on Trailhead.

Roll-up Summary With a Lookup Relationship

As mentioned above, roll-up summary fields can be created on the master through a master-detail relationship. So, it means that you cannot create a roll-up summary field if the relationship is a lookup. However, there are some ways to achieve this.

First of all, there is a package called Declarative Lookup Roll-up Summaries that lets you create a roll-up using a lookup field relationship. It has a simple configuration screen and it generates a code that will do the calculations. You can install it from here.

The second way is creating record-triggered flows for the calculations.

Record-Triggered Flows to Create Roll-Up Summary Fields

It is not possible to create a standard “roll-up summary” field but you can create a number field (or any other type of field according to your need) and populate it with a calculation using record-triggered flows. Calculation should be updated on create, update, and delete of the child record. Consequently, you have to create two record-triggered flows. One for create/update and another one for delete.

Read this post to learn more about flow types including record-triggered flows.

Lets see how to create a roll-up summary that shows the count of call logs related to a lead record. Before you start creating the flows, don’t forget to create a number field on the lead object (or on any object that you want).

After Create/Update Flow

First of all, when a call log (a task record with TaskSubtype=’Call’) related to a lead is created, we want the flow to update the number field. This is obvious. At the other hand, task record can be updated and its related lead (WhoId) can be changed. Because of this, our flow should run both on create and update.

Depending on your needs, you can decide not to run the flow on update (if reparenting is not allowed). Also, if you have a condition that cannot be met on create, you can decide to run the flow only on update.

1- Create a record-triggered flow on the task object. It should run after a record is created or updated. You don’t need to put a condition in the Start element, condition will be checked in a Decision.

2- Create a decision element to check if the task is new or its relationship field (WhoId) is changed. Since this example is to count the call logs, add another line of condition to check the TaskSubtype. 00Q in the first line means that the task is related to a Lead. It is important for this example because a task can be related to a contact as well. Only the tasks that are related to a lead object should fall to this path.

Add an extra decision outcome to check if the relationship is removed. If so, you have to skip the next part and move to step 7.

The resource called ‘ISNEW’ in the 3rd line is a Boolean formula that becomes true if the task is new. The formula itself is ISNEW()

Using this decision, we make sure that the flow will not execute any action if something else is updated on the task record. For example, if you change the status or update a text field on the task, flow will not recalculate the call log count.

3- Get all the call logs (tasks with TaskSubtype=’Call’) related to the same lead record. This is obvious because our goal is to count all of the call logs related to the same lead record.

4- Create a number variable with default value of zero and assign the record count of the collection that was created in the previous step. By doing this, we store the call log count in this variable. Its time to save this value to the lead record.

5- Update the number field that you created on the lead object with the value from the ‘TaskCount’ variable.

Now the roll-up summary field (its actually a number field but lets continue calling it a roll-up summary) on the lead record is updated. However, what if the task was reparented? We have to update the previous lead as well!

6- Create a decision to check if the previous WhoId was a lead. If so, you have to continue the flow.

7- Get all the call logs (tasks with TaskSubtype=’Call’) related to the previous lead record. You can use {!$Record__Prior.WhoId} to reference the previous lead record.

The rest is the same like we did before. Just don’t forget to update the previous lead record.

At the end, your flow should look like this.

Before Delete Flow

If a record (in this case it is a task record) is deleted, flow should run and do the calculation again. In this case, it is not an update so there cannot be a previous lead record. It would be great to put this logic in the same flow, but it is not possible to run a flow on create/update/delete. Because of this, you have to create another flow that will run on before delete.

1- Create a record-triggered flow on the task object. It should run before a record is deleted. This time, you can put the condition directly in the Start element, because there is no complex logic to check if something is updated or if the record is new.

It is enough to run the flow only if the task is related to a lead (it is the first line) and it is a call log (it is the second line).

2- Like in the previous flow, get all the call logs related to the same lead and then use an assignment to assign the record count. This part is the same.

3- Update the number field that you created on the lead object with the record count.

However, it is not the record count that you assigned to a variable in the previous step!

Since it is a before delete flow, the task is still in the system and it is related to the lead record, so the record count that you found is 1 more than the real count. Because of this, create a number formula variable with this formula: {!TaskCount}-1

At the end your flow should look like this.

You are ready to test the flows, just don’t forget to activate them.
Create/update/delete call logs related a lead record to see the flows in action.

In this example, we created two flows to build a roll-up summary field that shows a record count. According to your needs and conditions, you can build flows with different triggers (for example, just after create). It is possible to build the same logic to sum values or display min/max values from the related records. Since it is done with a flow, you can do any calculation that you need. Just keep in mind that if you need to sum values, you have to use a loop to add the values to a variable. If you are not sure about how to use loops, you can read this post.

Be the first to comment

Leave a Reply

Your email address will not be published.


*