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
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

trigger opportunityFactoryTrigger on Opportunity (after update) {

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;
          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.
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.

Mahesh DMahesh D
Hi SK,

Please check the below code:
public class opportunityFactoryTriggerHandler {
	public static void testDoAfterUpdate() {
		Opportunity opp = new Opportunity();
		opp.Name = 'Test Opp';
		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;