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
CTIT SidingCTIT Siding 

Saving Approval Comments

Hi All,
  I am a relatively new developer, and I am trying to write a trigger to save Approval Process Comments for use in reports. I have added 2 fields to an object: Approval Comments, which is a 1000 byte Long Text Area, and Copy Approval Comments, which is a Checkbox, defaulted to False. Whanever an approval or rejection occurs on this object, the Copy flag is set to True by the Approval Process, which causes my trigger to fire and copy all the comments from the Approval Process into the text field, and also to reset the Copy flag back to False.

  My problem is this: When the first approval occurs, I am not getting the comments in the text field. Using debug statments in the trigger, it appears that they are not available yet for some reason. If I manually set the Copy flag to True, the comments will copy correctly. Also, when the second approval occurs, all the comments are picked up and copied. 

  Has anyone else tried to do this? If so, did you encounter the same issue? Here is the trigger code:
trigger ROC_Triggers on ROC__c (before insert,after insert,after update,after delete) {
 
   if(Trigger.isAfter){
    if(Trigger.isUpdate){

    // If Copy Comments flag is set and Comments were entered, then copy comments from Approval Process into Approval Comments field 
        List<ROC__c> ROCs = [Select r.Id, r.Approval_Comments__c, (Select ActorId, Comments, CreatedDate From ProcessSteps order by CreatedDate) 
                      From ROC__c r where r.Copy_Approval_Comments__c = True];
     
        if (ROCs.size()>0)
         {ROC__c rc=ROCs[0];
          rc.Approval_Comments__c = '';
            for (ProcessInstanceHistory ps : rc.ProcessSteps)
                if (ps.Comments != null){
                    if (ps.Comments != ' '){
					    User u = [SELECT name FROM User WHERE Id = :ps.ActorId Limit 1];
                        string username = u.name; 	
                        rc.Approval_Comments__c += '\n' + username + ': ' + ps.Comments;          
               			System.debug('***********************************************Comments copied:' + ps.comments);	   
                    }}}
		else 
           {System.debug('*******************************************No comments found to copy');}
        
    List<ROC__c> ROCsToUpdate = new List<ROC__c>();
    
   // Iterate over the ROCs
    for(ROC__c ROC : ROCs) {      

   // Update the Approval Comments and reset the Copy flag
        ROC.Copy_Approval_Comments__c = False;
        ROCsToUpdate.add(ROC);
        }
    
    // Perform DML on a collection
     try
        {   
            update ROCsToUpdate;
            System.debug('******************************************** Record updated');
        }
        catch(exception e)
        {
            string message = 'Unable to update record - ' + e.getMessage();
            System.debug('******************************************** Error: ' + message);
        }        
      }
   }    
}


Also, if anyone has any tips on how to improve the code, it would be welcome, as I am figuring this out as I go.

Thank you,
Stave A.