0

I want to pull all approval/rejection process comments to put in my customized report. However my code didn`t work like that way. This code only capture a comment before a final approval. For example if I have 4 persons to approve/reject a application, this code only capture comment from 3rd person. May I know how to fix this code?

trigger pullCommentsApproval on Return_Shipment__c (before update) {
    
    if(trigger.isUpdate){
        List<Return_Shipment__c> opptyList =  [Select id,
                                               (Select Id, 
                                                IsPending, 
                                                ProcessInstanceId, 
                                                TargetObjectId, 
                                                StepStatus, 
                                                OriginalActorId, 
                                                ActorId, 
                                                RemindersSent, 
                                                Comments, 
                                                IsDeleted, 
                                                CreatedDate, 
                                                CreatedById, 
                                                SystemModstamp 
                                                FROM ProcessSteps
                                                ORDER BY CreatedDate DESC) 
                                               From Return_Shipment__c
                                               WHERE Id IN : Trigger.new];
        
        if(opptyList.size() > 0){
            
            for(Return_Shipment__c opp : opptyList){
                
                for(Return_Shipment__c opp1 : Trigger.new) {
                    
                    //check copy comment is true
                    if(opp.id == opp1.id && opp1.Final_Approved__c) {
                        
                        if (opp.ProcessSteps.size() > 0) {
                            
                            opp1.Approver_Comments__c = opp.ProcessSteps[0].Comments;
                            //opp1.Final_Approved__c = false;
                            
                        }
                        
                    }
                    
                }
            }
        }   
    }  
}


1 Answer 1

0

It looks like you're specifically only getting the last approval comment by saying "opp.ProcessSteps[0].Comments" here:

if (opp.ProcessSteps.size() > 0) {
    opp1.Approver_Comments__c = opp.ProcessSteps[0].Comments;
}

You would want to loop through each approval step to get the comment for all steps. If your "Approver_Comments__c" field is a long text field, you can add "\n" at the end to put each comment on a new line:

if (opp.ProcessSteps.size() > 0) {
    for (ProcessInstanceStep processStep : opp.ProcessSteps) {
        opp1.Approver_Comments__c += processStep.Comments + '\n';
    }
}

On an aside, for better performance, you would only need to loop through the opptyList array (and not both arrays) as you can set the value in your Trigger data using Trigger.newMap like this:

Trigger.newMap.get(opp.Id).Approver_Comments__c += processStep.Comments + '\n';
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .