function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Forrest MulduneForrest Muldune 

Need Help with Trigger Rollup Summary on Parent and Child Objects

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

REQUIREMENT
 
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
 
OBJECTS
Opportunity (parent object)
Matter__c  (Child object) custom object.
 
 
OPPORTUNITY FIELD
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
 

WHAT I HAD DONE
 
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
*/

ctx.add(
            new LREngine.RollupSummaryField(
                                            Schema.SObjectType.Opportunity.fields.Engagement_Total_Fees__c,
                                            Schema.SObjectType.Matter__c.fields.Total_Fees__c,
                                            LREngine.RollupOperation.Sum 
                                         )); 
/* 
      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?
SalesFORCE_enFORCErSalesFORCE_enFORCEr
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.
https://appexchange.salesforce.com/listingDetail?listingId=a0N3000000B45gWEAR
nitin sharma 190nitin sharma 190
Check below given link
http://www.forcewizard.com/blog/rollup-summary-using-process-builder-and-flow
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.