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 

Please help me write a test class for the trigger code

Scenario : This trigger with the apex class helps in calculating roll up of average (cost, amount and probability ) from product child object to sub-oppty parent object. Please help me with the test class for this . Your help will be greatly helpful.

trigger AvgCalculateTrigger on Sub_Opportunity_Product__c (after insert, after update, after delete) {
    
    if( Trigger.isInsert || Trigger.isAfter || Trigger.isDelete ){
        if( Trigger.isAfter ){
           AvgCalculate.avgCalculate();
           AvgCalculate.avgCalculate1();
            AvgCalculate.avgCalculate2();
           
           
        }
    }
        
}





Apex Class

public class AvgCalculate {

    public static void avgCalculate (){
        Set<Id> subopptyIds = new Set<Id>();
        List<Sub_Opportunity__c> subopptyToUpdate = new List<Sub_Opportunity__c>();
      
        List< Sub_Opportunity_Product__c> sub = Trigger.isInsert || Trigger.isUpdate ? Trigger.New : Trigger.Old;
        for(Sub_Opportunity_Product__c  ct : sub ){
            subopptyIds.add( ct.Sub_Opportunity__c /*lookup field value of utilityrecord */ );
        }

        for( AggregateResult ag : [ SELECT Sub_Opportunity__c, AVG( Cost__c ) avg FROM Sub_Opportunity_Product__c
                                    GROUP BY Sub_Opportunity__c ] ){
             subopptyToUpdate.add( new Sub_Opportunity__c( 
                Id = (Id)ag.get('Sub_Opportunity__c'), 
                 Actual_Cost2__c = (Decimal)ag.get('avg') ) );                            
        }
         
      
         
         
              if(subopptyToUpdate.size() > 0 ){
            update  subopptyToUpdate;
        }
        
       
    }
    public static void avgCalculate1 (){
        Set<Id> subopptyIds1= new Set<Id>();
        List<Sub_Opportunity__c> subopptyToUpdate1 = new List<Sub_Opportunity__c>();
      
        List< Sub_Opportunity_Product__c> sub1 = Trigger.isInsert || Trigger.isUpdate ? Trigger.New : Trigger.Old;
        for(Sub_Opportunity_Product__c  ct : sub1 ){
            subopptyIds1.add( ct.Sub_Opportunity__c /*lookup field value of utilityrecord */ );
        }

        for( AggregateResult ag1 : [ SELECT Sub_Opportunity__c, AVG( Amount__c ) avg FROM Sub_Opportunity_Product__c
                                    GROUP BY Sub_Opportunity__c ] ){
             subopptyToUpdate1.add( new Sub_Opportunity__c( 
                Id = (Id)ag1.get('Sub_Opportunity__c'), 
                 Actual_Revenue2__c = (Decimal)ag1.get('avg') ) );                            
        }
         
         if(subopptyToUpdate1.size() > 0 ){
            update  subopptyToUpdate1;
        }
       
    }  
      public static void avgCalculate2 (){
        Set<Id> subopptyIds2= new Set<Id>();
        List<Sub_Opportunity__c> subopptyToUpdate2 = new List<Sub_Opportunity__c>();
      
        List< Sub_Opportunity_Product__c> sub2 = Trigger.isInsert || Trigger.isUpdate ? Trigger.New : Trigger.Old;
        for(Sub_Opportunity_Product__c  ct : sub2 ){
            subopptyIds2.add( ct.Sub_Opportunity__c /*lookup field value of utilityrecord */ );
        }

        for( AggregateResult ag2 : [ SELECT Sub_Opportunity__c, AVG( Probability__c ) avgprob FROM Sub_Opportunity_Product__c
                                    GROUP BY Sub_Opportunity__c ] ){
             subopptyToUpdate2.add( new Sub_Opportunity__c( 
                Id = (Id)ag2.get('Sub_Opportunity__c'), 
                 Probability__c = (Decimal)ag2.get('avgprob') ) );                            
        }
         
         if(subopptyToUpdate2.size() > 0 ){
            update  subopptyToUpdate2;
        }
       
    }  
  
 }
 
PriyaPriya (Salesforce Developers) 
The developer community recommends providing any attempts/code you've started, any errors you're getting, or where exactly you're struggling in achieving this while posting a question.