+ Start a Discussion
Forrest MulduneForrest Muldune 

Need Help with Trigger Rollup Summary on Parent and Child Objects

I woul appreciate any help with my requirement below .  I am kind of new in coding and I would appreciate any assitance

When Opportunity record is connected to one or more Matter__c  record, Sum all currency values in the Total_Fees__c currency field and add Sum total value in the Engagement_Total_Fees__c field in the related Opportunity record
Opportunity (parent object)
Matter__c  (Child object) custom object.
Engagement_Total_Fees__c - currency (16,2) custom field
MATTER__C field
Total_Fees__c - currency (16,2) custom field
Deal__c  - Lookup(Opportunity) custom field

Below is a trigger I created based on website https://github.com/abhinavguptas/Salesforce-Lookup-Rollup-Summaries .
I am requesting if you can review my code because I received the error below
“Error: Compile Error: Invalid type: LREngine.Context at line 26 column 33”
 The data on line 26 is “LREngine.Context ctx = new LREngine.Context(Opportunity.SobjectType, // parent object

Trigger Below

/* Updated version on 17 June 16


trigger OppRollupOnEngagement on Matter__c (after insert, after update, 
                                        after delete, after undelete) 

// modified objects whose parent records should be updated
     {Matter__c[] objects = null;

if (Trigger.isDelete) {
         objects = Trigger.old;

      Handle any filtering required, specially on Trigger.isUpdate event. If the rolled up fields
      are not changed, then please make sure you skip the rollup operation.
      We are not adding that for sake of similicity of this illustration.
        objects = Trigger.new;

      First step is to create a context for LREngine, by specifying parent and child objects and
      lookup relationship field name
     LREngine.Context ctx = new LREngine.Context(Opportunity.SobjectType, // parent object
                                            Matter__c.SobjectType,  // child object
                                            Schema.SObjectType.Matter__c.fields.Deal__c // relationship field name

      Next, one can add multiple rollup fields on the above relationship. 
      Here specify 
       1. The field to aggregate in child object
       2. The field to which aggregated value will be saved in master/parent object
       3. The aggregate operation to be done i.e. SUM, AVG, COUNT, MIN/MAX

            new LREngine.RollupSummaryField(
      Calling rollup method returns in memory master objects with aggregated values in them. 
      Please note these master records are not persisted back, so that client gets a chance 
      to post process them after rollup

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

     // Persiste the changes in master
     update masters;


I would appreciate the help . 
nitin sharma 190nitin sharma 190
Did u try using process builder to do rollup between parent and child objects?
Forrest MulduneForrest Muldune
Yes I did, but it did not work because I have a lookup field on Matter__c custom object, not a Master Detail relationship. 

Can I use Process builder for my requirement?
Use DLRS, it's super easy to set any type of enhanced roll up just by using configuration. It auto generates trigger and class for you. I use it all the time.
nitin sharma 190nitin sharma 190
Check below given link
Forrest MulduneForrest Muldune
I am just checking to see if anyone has a solution to my request. I tried using the process builder & Flow method, but I would rather use a Trigger for the Undelete function.

Any help will be appreciated.