0

I want to pull comments from the approval report history (only final approval). By default, Final_Approved__c is false. What I want is, if Final_Approved__c is true, the final comment will be displayed on the customized report.

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) {               
                            if (opp1.Final_Approved__c==true){
                                opp1.Approver_Comments__c = opp.ProcessSteps[0].Comments + ' ' + opp1.Approval_Status__c;
                            }                          
                    }                        
                }                     
            }        
  }         
    }  
}

Here is my approval history, what I want is, I only want to pull the last comment. In this approval history, I want approval4 comments(the last one).

enter image description here

However, this code display this result in report:

enter image description here

But this report takes 1 before the final comment. May I know how I can alter this code?

1 Answer 1

1

you must be marking the opp field Final_Approved__c as true on last approval by approval field update.

In this case your approval field update is hapenning before approval data is commited to the data base i.e before save point.

to fix this, in case when your field Final_Approved__c is true you can run the future method from the trigger which will run after the approval data is commited to the database and get the correct approver comment.

7
  • May I know where I can put the future method? is it in if Final_Approved__c==true statement?
    – Atiqah
    Commented Apr 13, 2021 at 1:49
  • Is it I need to do like this? Invoke Future Methods through Apex Trigger for web service callout? But I need to change to get the approval comment
    – Atiqah
    Commented Apr 13, 2021 at 2:01
  • Yes like that only but without callout =true as you are not doing a web service callout, you can fed the ids of all the records where final approved is changed in to the future method and then do the update there Commented Apr 13, 2021 at 11:31
  • You can check future methods documentation here as well developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… Commented Apr 13, 2021 at 11:52
  • Alright, thank you! I will mark this as an answer after I implement this :-)
    – Atiqah
    Commented Apr 13, 2021 at 13:45

You must log in to answer this question.

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