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
HARSHIL U PARIKHHARSHIL U PARIKH 

I need to stop this trigger firing twice. I implemented that extra RecursionBlocker method but it is still firing twice.

Apex Class:
public class RecursionBlocker{
   public static Boolean flag = true;
}
Trigger: I am creating an event record once the record is approved.
Trigger CreatingAnEvent on Approval_Requests__c(After update){
 
     If(RecursionBlocker.Flag = true){

     RecursionBlocker.Flag = false;
     
      Event evt = New Event();
      If(Trigger.isUpdate)
      {
            For(Approval_Requests__c AR: Trigger.New)
            {
                                           
                   If(AR.Approval_Status__c == 'Approved') // Then Create an event record.
                   { 
                       evt.StartDateTime = AR.Start_Date_Time__c;
                       evt.ACA_Location__c = AR.ACA_Location__C;
                       evt.Type = AR.Type__C;
                       evt.Description = AR.Situation__C;
                       evt.Subject = AR.Subject__C;
                       evt.OwnerID =  String.valueOf(AR.OwnerId);  // "Owner" is an assign to
                       evt.Whatid = AR.id;                        // "what" is a relate to
                       evt.DurationInMinutes =   0; 
                       evt.Automatically_Created_Event__c = TRUE;
                       evt.Approved__C = TRUE;
                       evt.Other_Location__C = AR.Other_Location__C;
                       evt.Organization__C = AR.Account__C;
                       evt.whoid = AR.Contact__c;                 // Who is Name field label on an Event and it's looking up to 
                                                                  //     either Lead or Contact. For our purpose of creation, we are looking up 
                                                                  //       to Contact. Id to Id match. 
                     insert Evt;
                                              
                   }
                   else{
                       // Do Nothing
                   }
                   
            }   
   
        }
    }
}
Now, once the record gets approved then an event record gets created -- which is fine!
But when I update the record again, then new event is created which is needed to be stop.
Thak You for the help!


 
Best Answer chosen by HARSHIL U PARIKH
Amit Chaudhary 8Amit Chaudhary 8
Try to use Trigger.oldMap for same

Try to update your code like below
Trigger CreatingAnEvent on Approval_Requests__c(After update)
{
    If(RecursionBlocker.Flag = true)
	{

     RecursionBlocker.Flag = false;
     
      Event evt = New Event();
      If(Trigger.isUpdate)
      {
            For(Approval_Requests__c AR: Trigger.New)
            {
					Approval_Requests__c AROLD = Trigger.OldMap.get(AR.id);							
                   If(AR.Approval_Status__c == 'Approved' && AR.Approval_Status__c != AROLD.Approval_Status__c ) // Then Create an event record.
                   { 
                       evt.StartDateTime = AR.Start_Date_Time__c;
                       evt.ACA_Location__c = AR.ACA_Location__C;
                       evt.Type = AR.Type__C;
                       evt.Description = AR.Situation__C;
                       evt.Subject = AR.Subject__C;
                       evt.OwnerID =  String.valueOf(AR.OwnerId);  // "Owner" is an assign to
                       evt.Whatid = AR.id;                        // "what" is a relate to
                       evt.DurationInMinutes =   0; 
                       evt.Automatically_Created_Event__c = TRUE;
                       evt.Approved__C = TRUE;
                       evt.Other_Location__C = AR.Other_Location__C;
                       evt.Organization__C = AR.Account__C;
                       evt.whoid = AR.Contact__c;                 // Who is Name field label on an Event and it's looking up to 
                                                                  //     either Lead or Contact. For our purpose of creation, we are looking up 
                                                                  //       to Contact. Id to Id match. 
                     insert Evt;
                                              
                   }
                   else{
                       // Do Nothing
                   }
                   
            }   
   
        }
    }
}

Let us know if this will help you

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Try to use Trigger.oldMap for same

Try to update your code like below
Trigger CreatingAnEvent on Approval_Requests__c(After update)
{
    If(RecursionBlocker.Flag = true)
	{

     RecursionBlocker.Flag = false;
     
      Event evt = New Event();
      If(Trigger.isUpdate)
      {
            For(Approval_Requests__c AR: Trigger.New)
            {
					Approval_Requests__c AROLD = Trigger.OldMap.get(AR.id);							
                   If(AR.Approval_Status__c == 'Approved' && AR.Approval_Status__c != AROLD.Approval_Status__c ) // Then Create an event record.
                   { 
                       evt.StartDateTime = AR.Start_Date_Time__c;
                       evt.ACA_Location__c = AR.ACA_Location__C;
                       evt.Type = AR.Type__C;
                       evt.Description = AR.Situation__C;
                       evt.Subject = AR.Subject__C;
                       evt.OwnerID =  String.valueOf(AR.OwnerId);  // "Owner" is an assign to
                       evt.Whatid = AR.id;                        // "what" is a relate to
                       evt.DurationInMinutes =   0; 
                       evt.Automatically_Created_Event__c = TRUE;
                       evt.Approved__C = TRUE;
                       evt.Other_Location__C = AR.Other_Location__C;
                       evt.Organization__C = AR.Account__C;
                       evt.whoid = AR.Contact__c;                 // Who is Name field label on an Event and it's looking up to 
                                                                  //     either Lead or Contact. For our purpose of creation, we are looking up 
                                                                  //       to Contact. Id to Id match. 
                     insert Evt;
                                              
                   }
                   else{
                       // Do Nothing
                   }
                   
            }   
   
        }
    }
}

Let us know if this will help you
This was selected as the best answer
bainesybainesy
The approval staus is still == 'Approved' after the intial event is created. You will need to create a flag field ('Event Created') that is a check box that persists that an event has already been created. So, your trigger would then check for AR.Approval_Status__c == 'Approved' && AR.Event_Created__c == false.
HARSHIL U PARIKHHARSHIL U PARIKH
Thank you guys for the knowledge!