Lookup Rollup

There are a lot of functionalities readily available in Salesforce. One of my favorites is Rollup summary fields; using this, it becomes easy to find out the sum, count and other aggregate functions of the children on a parent. I am glad Salesforce built this, but the catch is they need to be in a master-detail relationship, now that’s unfair. I was stuck on a requirement of the same functionality on a lookup relationship. Since the conversion of lookup to master detail was not possible at that time, the repository god Github gave me a way out.

Thanks to GitHub and its members for providing me with a repository named LR Engine. LR abbreviated as Lookup Rollup is a functionality which is capable of offering Rollup summary fields on Lookup relationship. It’s pretty easy to use; follow the steps below to implement it.

Prerequisites:  Copy the code from the given URL, create a new class in your Salesforce org, and we are ready to get started.   

https://github.com/abhinavguptas/Salesforce-Lookup-Rollup-Summaries/blob/master/classes/LREngine.cls

Assumption:

Consider there is an object named – Internet_Plans__c and an object named – Company_Users__c. Connect both through lookup relation named: Plan__c.

Internet_Plans__c consists of information about the various plans that the company offers, and Company_Users__c consists of details about customers of the company.

There is a requirement to calculate the number of users who have bought a particular plan on a field named – Number_of_users__c which is available on Internet_Plans__c object. Let’s see the below code snippet to accommodate this requirement.

Step 1 :

Reference the newly created class and declare the objects with their relation.

LREngine.Context ctx =

new LREngine.Context(

Internet_Plans__c.SobjectType, // parent object

Company_Users__c.SobjectType, // child object

Schema.SObjectType.Company_Users__c.fields.Plan__c); // relationship field name

Step 2 :

Add a method to declare the fields to be used, populated and the operations to be performed.

ctx.add(

new LREngine.RollupSummaryField(

Schema.SObjectType.Internet_Plans__c.fields.Number_of_users__c, // Parent field in which value should be populated

Schema.SObjectType.Company_Users__c.fields.Id, // Child field taken as a reference

LREngine.RollupOperation.Count)); //operation to be performed

Step 3:

Get the list of Child records and calculate the desired operation using the roll-up method

Company_Users__c[] objects = list_of_child_records;

Sobject[] masters = LREngine.rollUp(ctx, objects);

Step 4:

Update the list and make a shout!

update masters;

Note: You can add this code in a batch class, apex class or even a trigger as it depends on your requirement. If you choose to write a trigger, let it be on a child object so the list_of_child_records would be trigger.new or if you want it some other way, remember to DML a child records list only.

Don’t forget to get the readily built test class for the newly created class here:

https://github.com/abhinavguptas/Salesforce-Lookup-Rollup-Summaries/blob/master/classes/TestLREngine.cls.

Cheers if it made your day!