+ Start a Discussion
HARSHIL U PARIKHHARSHIL U PARIKH 

Trigger is not getting fired. It fires only when I individually update the record.

Hello Developers,

I have a trigger on object called Approval__C which creats an event record when Approval_Status__C is Approved.

Trigger is not automatically getting fired when approver approves the record but when I update the 'Approved' record manually then trigger gets fired. How do I make this trigger fire automatically when Approval_Status__c is Approved.
Trigger:
Trigger CreatingAnEvent on Approval__c(After update){
    
      Event evt = new Event();
    
      If(Trigger.isUpdate || Trigger.isUndelete)
      {
            For(Approval__c AP: Trigger.old)
            {
               if(AP.Approval_Status__c != 'Approved')
               {
                   // Nothing    
               }                            
               else
               {
                   evt.ACA_Location__C = AP.Location__c;
                   evt.StartDateTime = AP.Start_Time__c;
                   evt.EndDateTime = AP.End_Time__c;
                   evt.Type = AP.Type__c;
                   evt.Description = AP.Situation__C;
                   evt.Subject = AP.Subject__C;
                   evt.OwnerID =  String.valueOf(AP.OwnerId);                              // "Owner" is an assign to
                  // evt.What = AP.RecordID;                                                // "what" is a relate to 
                  evt.DurationInMinutes =   0; 
                 insert evt;                          
               }
               
            }    
    }
}
Thank you for the help!


 
James LoghryJames Loghry
My guess is that  it actually *is* getting fired.  Looking at your code, you're checking values against Trigger.old, instead of Trigger.new.  The way your code is written, if the Approval status is "Approved", and THEN the record is updated, an event will be created.

Try changed "Trigger.old" to "Trigger.new".  Also, you'll want to move the "insert evt" statement outside of your for loop and bulkify it, in order to avoid Salesforce's governor limits.

Doing both items I suggested, you should get something similar to:
 
Trigger CreatingAnEvent on Approval__c(After update){
    
      List<Event> evts = new List<Event>();
    
      If(Trigger.isUpdate || Trigger.isUndelete)
      {
            For(Approval__c AP: Trigger.new)
            {
               //Changed to use Trigger.new instead of trigger.old
               //Also, added check of Trigger.old to make sure the logic only happens when
               //the record changes from not approved to approved.
               if(Trigger.oldMap.get(AP.Id).Approval_Status__c != 'Approved'
                    && AP.Approval_Status__c == 'Approved')
               {
                   evt.ACA_Location__C = AP.Location__c;
                   evt.StartDateTime = AP.Start_Time__c;
                   evt.EndDateTime = AP.End_Time__c;
                   evt.Type = AP.Type__c;
                   evt.Description = AP.Situation__C;
                   evt.Subject = AP.Subject__C;
                   evt.OwnerID =  String.valueOf(AP.OwnerId);                              // "Owner" is an assign to
                  // evt.What = AP.RecordID;                                                // "what" is a relate to 
                  evt.DurationInMinutes =   0;
                  evts.add(evt);                   
               }
               
            }    
    }
    insert evts;
}