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
MohiniMohini 

Need help with aggregate function.

- Please help me with the apex handler . I have 2 objects opportunity (parent) and sub -opportunity (child)objects . I have two fields in child object revenue and cost. There is a picklist field in sub opportunity called prod type. If the sub opportunity has similar  prod type, the revenue and cost should get averaged at opportunity object (Revenue field and cost field field) . For different prod type , the revenue should be sumed and displayed at oopportunity revenue field. Similarly for cost. Please help me how I can dynamically check the prod type and do average or sum for sub opportunity by product type and display in opportunity object revenue field.

Trigger handler:
public class SubOpportunityTriggerHandler {
    
    public void afterUpdate(List<Sub_Opportunity__c> newSubOpp){
        Set<ID> SubId = new Set<ID>();
        for (Sub_Opportunity__c sub : (List<Sub_Opportunity__c>)Trigger.New) {           
                SubId.add(sub.id);
            }    
        RollupCalculation(SubId);
        
    }
     public void afterinsert(List<Sub_Opportunity__c> newSubOpp){
        
        for (Sub_Opportunity__c opp : (List<Sub_Opportunity__c>)Trigger.New) {
                      
        }
    }
    public void afterdelete(List<Sub_Opportunity__c> newSubOpp){
        
        for (Sub_Opportunity__c opp : (List<Sub_Opportunity__c>)Trigger.New) {
                       
        }
    }
   
    public void afterundelete(List<Sub_Opportunity__c> newSubOpp){
        
        for (Sub_Opportunity__c opp : (List<Sub_Opportunity__c>)Trigger.New) {
                        
        }
    }  
    public static void RollupCalculation(Set<ID> subId){
        
    }
}
AnkaiahAnkaiah (Salesforce Developers) 
Hi Mohini,

What are the product types you want to calculate the revenue & cost fields?

If possible expain with example.

Thanks!!