0

currently I want to pull comments from the approval process form to a report. Here is my 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) {               
                    if(opp.id == opp1.id&& opp1.Final_Approved__c) {               
                        if (opp.ProcessSteps.size() > 0) {
                            opp1.Approver_Comments__c = opp.ProcessSteps[0].Comments;                  
                            opp1.Approver_Comments1__c = opp.ProcessSteps[1].Comments;                    
                            opp1.Approver_Comments2__c = opp.ProcessSteps[2].Comments;                           
                            opp1.Approver_Comments3__c = opp.ProcessSteps[3].Comments;                    
                            opp1.Approver_Comments4__c = opp.ProcessSteps[4].Comments;                           
                        }                           
                    }                        
                }                     
            }        
  }         
    }  
}

Output: enter image description here

As for now, I have 5 object fields to capture approval comments. One approval, one comment field. However, in real life, it is not necessary for all 5 people to need to approve the form. But this code won't show the comments until the fifth person approves them.

How I can alter this code to make no matter how many (max 5) people approve the form, the comments are still will be there (in the report?) For example, let say a form needs to be approved by 3 people, so all three comments will be shown in the report.

Final_Approved__c is a check box to ask approval is it she/ he is the final approval.

1 Answer 1

1

Your current Apex Trigger on Return_Shipment__c will only perform before update trigger when it detects that your "Final_Approved__c" field = TRUE.

Since you are SOQL-ing out the Return_Shipment__c Approval, just run an IF check for ProcessSteps.Comment != Null. In that IF loop, copy the ProcessSteps.Comment to Return_Shipment__c.Approver_Comments(1/2/3/4)__c.

You need to figure out how to map the correct approval comment to the right Return_Shipment__c.Approver_Comments via more IF checks (HINT: ProcessNode.Name)

Also you really need to consider abstracting your Apex Trigger logic into ReturnShipmentTriggerHandler & ReturnShipmentTriggerHandlerService - Something like this : https://www.forcetalks.com/blog/salesforce-apex-trigger-handler-the-developer-guide/

1
  • Hi, I just add if(ProcessSteps.Comment != Null) , but its shows me an error A non foreign key field cannot be referenced in a path expression: ProcessSteps. Btw, what is the difference ProcessSteps.Comment != Null and ProcessSteps.size() > 0? I thought both checking in if the comments is availble or not.
    – Atiqah
    Commented Apr 10, 2021 at 0:51

You must log in to answer this question.

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