**Introduction**

 

One of the questions that is asked almost every 3rd Day (my random estimation) has a solution that lies in having roll-up fields for lookup relationships.

There is an awesome app from Andrew Fawcett (at https://github.com/afawcett/declarative-lookup-rollup-summaries) that you can install to achieve the same, but for many reasons you may not want to install an app.
Personally, I feel that the app is superb but tends to include a lot of features that I will never use. Plus I don’t feel in-control of my Org ūüôā

So, this post is a guide for all Admins, who want to create lookup rollup fields.

** Warning **
As always Devs can keep away. #AwesomeAdmins in charge here.

**The setup**

For the purpose of this post, I have created the following 2 objects and fields respectively:

OBJECT 1:
img2img2-1
There are 4 rollup fields that I have created for the purpose of this demo. We can similarly have min, max, average etc.

OBJECT 2:

img3-1img3-2
A number and a text field that we will rollup.
A checkbox called ‘delete_it__c’ – stay with me on why we have it.

**The solution**

Important notes:
1. While changes in the rollup fields caused by the creation and the modification of ‘Lookup child’ records can be handled by PB, a delete action cannot
2. The deletion of a ‘Lookup child’ records can be handled in 2 methods, each having its pros and cons

2.1 Method1: Through profile permissions remove User access to delete the record. Use a quick action that will edit the record and mark delete_it__c = true. This will call the PB, which will call a flow that will eventually change the rollup fields and delete the record

2.2 Method2: Write a small before delete trigger to call the flow, which will change the rollup fields and delete the record

2.3 Method3: Create a VF page that has a controller that will call the flow, which will change the rollup fields and delete the record. Assign this VF page to the standard ‘delete’ button

Method1  and Method 3 have the below cons While Method2 is great because
Disables the User’s ability to delete the record
1. in related lists and list views
2. in Apex that uses with Sharing
User can use the delete action anywhere

 




Enough theory, lets get going now!!

** We will be using Method 2 here **

  1. The PB is on the child object

screenshot-40

2. Check for new child parents association. If true, call the flow with the below flow variablesscreenshot-41

screenshot (46).jpeg

3. Check if child parents association ahs changed i.e. lookup value on child has changed. If true, call the flow with the below flow variables

screenshot-42

screenshot (47).jpeg

4. Check for child parents has been disassociated, meaning that lookup field is blank now. If true, call the flow with the below flow variables

screenshot-43

screenshot (48).jpeg

5. Check if child recrd was deleted **Use this incase you are using Method 1 or Method 3 discussed at the beginning of the blog. If true, call the flow with the below flow variables

screenshot-44screenshot (49).jpeg

6. Check if value of fields change on the child record. If true, call the flow with the below flow variables

screenshot-45screenshot (50).jpeg

Now the flow:

screenshot (52).jpeg

And finally for using Method2, the apex trigger:

trigger callFlowOnDelete on Lookup_Child__c (before delete) {
   public Flow.Interview.Update_Lookup_rollups myFlow;
   Map<String, Object> myMap = new Map<String, Object>();
   myMap.put('LookupChildID_New', trigger.old);
   Lookup_Parent__c l_parent = new Lookup_Child(trigger.oldmap.get(Lookup_Parent__r.Id));
   myMap.put('LookupParentID_Old', l_parent.id);
   myMap.put('PBInput','6');
   myFlow = new Flow.Interview.Update_Lookup_rollups(myMap);
   myFlow.start();
}

And that’s it folks.

Advertisements