+ Start a Discussion
Forrest MulduneForrest Muldune 

Trigger - Approval Process - Email Alert

All,

I created a trigger ( view below)  that will take values from  the Comments field when an Approver either Approves or Rejects a record, the values in the comment field  will then be inserted in the  Approver_Comment__c long text field located in the  matter__c custom object. However, When I created an email alert for the Final Approval Actions and Final Rejection Actions in the Approval Process, the email does not send me the current information in the Approver_Comment__c in the matter__c custom object, it only sends me the older value. Example if value "Test 1" already exist in the Approver_Comment__c and the approver approves the same record and writes "Test 2" in the Comments fieldd before approving or rejecting the record, after the approver selects the Approve or Reject button, the value "Test 1"  will be send in the email alert , not the value "Test 2". I appreciate the help. I want the most current value in the comments field which in this siutation  is "Test 2" to be sent via the email alert. 

I could appreciate it if someone could assist me on this. 
 
 
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;
               
                           }
 
                        }
                
                    }
               }
             }  
        } 
    }
 
James LoghryJames Loghry
This could be due to a few issues:
  1. You're using ProcessSteps[0] in your Approver_Comment__c assignment.  Looks like you might need to order the ProcessSteps by CreatedDate Desc or check that that ProcessStep was for the approval / rejection step and not something else.
  2. You're querying existing data which hasn't been committed yet.  It looks like you're handling this OK, but perhaps query the ProcessSteps directly (e.g. [Select Comments From ProcessStep Where ParentId in :Trigger.newMap.keySet()] instead?
Hopefully one of those two items helps.
Forrest MulduneForrest Muldune
James,. 

I tried to modify this trigger, but I was unsuccessful in modifying it. do you have an example of how I can modify this coding? 

I am using this trigger from approval or rejection of records.

I followed your advice in step 2, but I am receiving errors, view below.

trigger TriggerApprover on Matter__c (before update) {
    
       if(trigger.isUpdate){
             List<Matter__c> MatterList =  [Select id,
                                                   (Select Id,  
                                                         Comments,  
                                                    FROM ProcessSteps
                                                ORDER BY CreatedDate DESC) 
                                                    From Matter__c
                                                WHERE ParentId in :Trigger.newMap.keySet()] ];

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

                        }
                 
                    }
               }
             }   
        }  
    }