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
Ravi23Ravi23 

Need to capture approval process comments on custom field

I have a requirement to capture the approval process comments on a custom field we just have one step for approval process. Can some one guide me to achieve the same.
SKolakanSKolakan
Ravi23,

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

Option1: Use apex to update object. See if you can run the code with in 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 above query and manually create your object import file by filling comments field from process instance data.
Santhosh Gaddam 3Santhosh Gaddam 3
Hi Skolakan.

"process definition Id"?

//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();
SKolakanSKolakan
@Santosh
You can query it using
SELECT Id FROM ProcessDefinition
Where State = 'Active' and TableEnumOrId = <Your object API name>

 
Santhosh Gaddam 3Santhosh Gaddam 3
Hi Sreeni, Exactly My requirement is same as this & Is this Trigger or Apex Class? [image: Mailtrack] Sender notified by Mailtrack 08/23/19, 09:50:07 PM