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
Padma rudraPadma rudra 

Hi everyone, How to store Last approval process comments into custom field on a case object?

AbhishekAbhishek (Salesforce Developers) 
Hi Padma,

You can do it in many ways. Here are a couple of options

Option1: Use apex to update the object. See if you can run the code within governor limits.

Here is the pseudo-code to get you started. 
-----------------------------------------------

//Get all approval process records from an approval process definition
List<ProcessInstance> instances = [SELECT Id,TargetObjectId,(SELECT Id, StepStatus, Comments FROM Steps) FROM ProcessInstance Where ProcessDefinitionId = '[Your process definition Id]'];


 Set<String> objectIds = new Set<String>();

//Create a set of object Ids which has process instance
    for(ProcessInstance pi:instances){
        objectIds.add(pi.TargetobjectId);
    }
 
//Query for related records
Map<Id,YOUR_OBJECT__C> yourObjectMap = new Map<Id,YOUR_OBJECT__C>([Select Your_Comments_Field__c from YOUR_OBJECT__C Where Id in:objectIds ]);

//populate object's comment field from approval comments
    for(ProcessInstance pi:instances){
       for (ProcessInstanceStep step : pi.Steps) {
         if(step.Status == 'Approved') {
            yourObjectMap.get(pi.TargetObjectId).Your_Comments_Field__c = step.Comments;
         }
       }
    }

//Update your object
update yourObjectMap.values();

-----------------------------------------------------

Option2: Export the process instance data from the above query and manually create your object import file by filling the comments field from process instance data.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.


Thanks.