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
Ryan Bell 21Ryan Bell 21 

Roll Up summary in Opportunity

Is there a way to have a tierd oppotunity  within an opportunity? 

As an example, Lets say my Account is Macdonalds, and I have an opportunity as Macdonalds Location number X, 

With in this opportunity I want to create several sub opportunities and have them roll up to the total opportunity at Macdonalds Location number X. 

At this location I want a sub opportunity to sell 
Coke at Macdonalds Location number X
Burger patties at "  "
French Fries at "  " 
Ect, 

I dont want these all as seperate opportunites because i want them to be all assoicated with the one Location Opportunity, and ultimately the account is Macdonalds. 

The other thing is that the sub opportunites are not a product, rather a service that will always have different rates etc. 

Any help would be most appricated. 
Prakhar Saxena 19Prakhar Saxena 19
Hi Ryan,

You can create a Custom Object like 'Child Opportunity' which will have a Lookup Relationship to Opportunity. Add fields like Item, Price and Opportunity__c (Lookup field) to this object.

You can then create a field like 'Sum of Child Opportunities' on the Parent Object, Opportunity. Any Price values added/updated/deleted in the Child Opportunity record will reflect in the related Opportunities' Sum of Child Opportunities field. 

(This functionality is exactly same as a Roll up Summary field which can be achieved by creating a Master-Detail relationship between Opportunity and Child Opportunity. However, I guess you cannot add another Master-Detail relationship on Opportunity (apart from Opportunity Product).
https://success.salesforce.com/answers?id=906300000019MPAAA2

In that case, you will have to achieve this Roll-up Summary functionality through some other means, either by using a Trigger or a Process Builder.

I tried with Trigger and it works fine.
 
trigger ChildOpportunityTrigger on Child_Opportunity__c (after insert, after update, after delete) {
    
    ChildOpportunityHandler handler = new ChildOpportunityHandler();
    
    if(Trigger.isAfter){
        
        if(Trigger.isInsert){
            handler.addInSumOfChildOppField(Trigger.New);
        }
        
        if(Trigger.isUpdate){
            handler.updateSumOfChildOppField(Trigger.Old, Trigger.New);
        }
        
        if(Trigger.isDelete){
            handler.deleteFromSumOfChildOppField(Trigger.Old);
        }
    }
}
 
public class ChildOpportunityHandler {
    
    public void addInSumOfChildOppField(List<Child_Opportunity__c> newChildOpportunities){
        
        for(Integer i = 0; i<Trigger.size; i++){
            
            if(newChildOpportunities.get(i).Opportunity__c != null){
                
                Decimal sumOfChildOpportunities;
                Opportunity opp = new Opportunity();
                opp.Id = newChildOpportunities.get(i).Opportunity__c;
               
                Opportunity oppor = [SELECT Sum_of_Child_Opportunities__c FROM Opportunity WHERE ID =: opp.Id]; 
                
                if(oppor.Sum_of_Child_Opportunities__c != null){
                   sumOfChildOpportunities = oppor.Sum_of_Child_Opportunities__c; 
                }
                else{
                    sumOfChildOpportunities = 0.00;
                }
                opp.Sum_of_Child_Opportunities__c = sumOfChildOpportunities + newChildOpportunities.get(i).Price__c;
                update opp;
                
            }
        }
    }
    
    public void updateSumOfChildOppField(List<Child_Opportunity__c> oldChildOpportunities, List<Child_Opportunity__c> newChildOpportunities){
        
        for(Integer i = 0; i<Trigger.size; i++){
            
            if(oldChildOpportunities.get(i).Price__c != newChildOpportunities.get(i).Price__c && newChildOpportunities.get(i).Opportunity__c != null){
                
                Opportunity opp = new Opportunity();
                opp.Id = newChildOpportunities.get(i).Opportunity__c;
                
                Opportunity oppor = [SELECT Sum_of_Child_Opportunities__c FROM Opportunity WHERE ID =: opp.Id]; 
                
                opp.Sum_of_Child_Opportunities__c = oppor.Sum_of_Child_Opportunities__c - oldChildOpportunities.get(i).Price__c;
                opp.Sum_of_Child_Opportunities__c += newChildOpportunities.get(i).Price__c;
                update opp;
                
            }
        }
    }
    
    public void deleteFromSumOfChildOppField(List<Child_Opportunity__c> oldChildOpportunities){
        
        for(Integer i = 0; i<Trigger.size; i++){
            
            if(oldChildOpportunities.get(i).Opportunity__c != null){
                
                Opportunity opp = new Opportunity();
                opp.Id = oldChildOpportunities.get(i).Opportunity__c;
                
                Opportunity oppor = [SELECT Sum_of_Child_Opportunities__c FROM Opportunity WHERE ID =: opp.Id];
                
                opp.Sum_of_Child_Opportunities__c = oppor.Sum_of_Child_Opportunities__c - oldChildOpportunities.get(i).Price__c;
                update opp;
                
            }
        }
    }
}

Try adding records of Burger, French Fries, Coke in the Child Opportunities object along with their prices and associate it to an Opportunity. Check whether Opportunity's Sum of Child Opportunites field value is getting changed on every Insert/Update/Delete or not.

Let me know if there are any issues.