+ Start a Discussion
sk aleemsk aleem 

How to write Test Class for after update Trigger handler class and trigger

hi   can anybody help me to write Test class for this Trigger  and trigger handler class (after update)
custom object(Meeting__c in this checkbox field defult checked) 
scenario:if opportunity stage is 'closed own' or 'closed lost' then meeting__object check box is unchecked

opportunityTrigger
trigger opportunityFactoryTrigger on Opportunity (after update) {
  opportunityFactoryTriggerHandler.doAfterUpdate(trigger.new,trigger.newMap);
    
}

opportunityFactoryTriggerHandler​
public  class opportunityFactoryTriggerHandler {
     public static void doAfterUpdate(List<Opportunity> oppRecList, Map<Id, Opportunity> newMap){
      List<Meeting__c> mettingRecList1 = new List<Meeting__c>();
      List<Meeting__c> meetingRecListToBeUpdated = new List<Meeting__c>();
      mettingRecList1 = [SELECT Id, Opportunity__c FROM Meeting__c WHERE Opportunity__c IN :newMap.keySet()];
        for(Opportunity oppRec : oppRecList){
               if(oppRec.StageName == 'Closed Won' || oppRec.StageName == 'Closed Lost' ) {
                    for(Meeting__c meetingRec : mettingRecList1) {
                        if(meetingRec.Opportunity__c == oppRec.Id) {
                          meetingRec.Outcome_Flag__c = false;
                          meetingRecListToBeUpdated.add(meetingRec);
                        }    
                        
                    }
                    
                } 
         } 
          update meetingRecListToBeUpdated; 
    }     

}
Soumyaranjan Pati 10Soumyaranjan Pati 10
You just need to put the value of Opportunity insert first and then just Update, trigger will automatically Call.
Thanks
Mahesh DMahesh D
Hi SK,

Your Test Class should contains the following items.
     --> Write a Test Method
             --> Create one Opportunity Record.
             --> Using the Opportunity Record, create a Meeting__c record. Make sure that it is a child to the created opportunity.
             --> Update the Stage to 'Closed Won' or 'Closed Lost' on above created Opportunity.
             --> Retrieve the Meeting__c records using SOQL and write the Assertions.

Regards,
Mahesh
Mahesh DMahesh D
Hi SK,

Please check the below code:
 
@isTest
public class opportunityFactoryTriggerHandler {
	@isTest
	public static void testDoAfterUpdate() {
		
		Opportunity opp = new Opportunity();
		opp.Name = 'Test Opp';
		opp.CloseDate=System.Today();
                opp.StageName='Prospect';
		
		insert opp;
		
		Meeting__c me = new Meeting__c();
		me.Name = 'Test Meeting';
		me.Opportunity__c = opp.Id;
		insert me;
		
		opp.StageName = 'Closed Won';
		update opp;
	}
}

Regards,
Mahesh