+ Start a Discussion
Forrest MulduneForrest Muldune 

Apex Trigger - Approval Process - Approve or Reject

All,

I created an Apex Trigger below, but I assume it is firing after a user selects the "Submit" button. I would like for this trigger to fire after someone selects either the " Approve " button or " Reject " button.

I would appreciate your help 




trigger TriggerApprover on Matter__c (before update) {
   
       if(trigger.isUpdate){
             List<Matter__c> MatterList =  [Select id,
                                                   (Select Id,
                                                         IsPending,
                                                         ProcessInstanceId,
                                                         TargetObjectId,
                                                         StepStatus,
                                                         OriginalActorId,
                                                         ActorId,
                                                         RemindersSent,
                                                         Comments,
                                                         IsDeleted,
                                                         CreatedDate,
                                                         CreatedById,
                                                         SystemModstamp
                                                    FROM ProcessSteps
                                                ORDER BY CreatedDate DESC)
                                                    From Matter__c
                                                WHERE Id IN : Trigger.new];
 
             if(MatterList.size() > 0){
 
               for(Matter__c mat : MatterList){
             
                for(Matter__c mat1 : Trigger.new) {
                 
                         //check copy comment is true
                         if(mat.id == mat1.id && mat1.Copy_Comment__c) {
 
                           if (mat.ProcessSteps.size() > 0) {
                           
                         mat1.Approver_Comment__c = mat.ProcessSteps[0].Comments;
                         mat1.copy_comment__c = false;
               
                           }
 
                        }
                
                    }
               }
             }  
        } 
    }
 
AshlekhAshlekh
Hi,

You can sue Trigger.isBefore or Trigger.isAfter condition.

-Thanks
Ashlekh Gera
jjackson1.391016164401765E12jjackson1.391016164401765E12
We have custom code in our org that must perform similarly, e.g. if a certain type of record goes through the approval process, we want certain trigger code to run only after the record is approved.  The first thing we did was add a custom field to the object that goes through the approval process.  As part of the approval process, this field updates to a new value when a record is approved.  You can specify in your approval process which field (your custom field) to update and with what value.  In our case I think we use 'Billing Approved'.  When your custom field updates upon approval, this will fire off the trigger on your object.  You can call your code when trigger.isafter and trigger.isupdate.  For each record in the trigger you can verify that the value of your custom field in trigger.new is 'Billing Approved' (or whatever value you set it to within your approval process) and then allow the records having this value to continue on through your method.
Forrest MulduneForrest Muldune
AKG,

The updates you provided to me worked. 

The other problem I am experiencing is that I have an email alert that is connected to this approval. In the Trigger, it takes the "Comments" when the approver either approves or rejects the record on custom object matter__c . the comments are copied to the Approver_Comment__c (long text) on custom object matter__c, but the email is sent to the user containing the previous comments , not the updated comments. 

For example, if the values " 1,2,3" already exist in the Approver_Comment__c and the approver writes in new comments "4,5,6"  and approves or rejects the record, the updates comments "4,5,6" will not be sent to the email, the old values " 1,2,3"  will be sent only.