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
Avinash 35Avinash 35 

Hi will to capture the standard approval process comments to custom field.

I have creted the apex class and the call to trigger update and i have also try with before update but in that i occur the proccesinstance comments null
apex class:
 public static void updateComments(List<ASKWA_Documents__c> doclist){
        List<Id> docIds = New List <Id>();
        List<id>processId=new List<id>();
        for (ASKWA_Documents__c docObj: doclist){
            if(docObj.X2nd_Level_Approved__c == 'N'){
                System.debug('approval' +docObj.X2nd_Level_Approved__c);
                docIds.add (docObj.Id);
            }
        }
        List<ProcessInstance> instances = [SELECT Id, TargetObjectId FROM ProcessInstance WHERE TargetObjectId IN :docIds];
        System.Debug ('** instances: '+instances);
        Map<Id,Id> DocProcessMap = new Map<Id,Id>();
        docIds = New List <Id>();
        for(ProcessInstance piObj:instances){
            DocProcessMap.put (piObj.TargetObjectId,piObj.Id); 
            processId.add (piObj.id);
        }
        System.Debug ('** mapa1: '+DocProcessMap);
        System.Debug ('** processId: '+processId);
        string commentStr=''; 
        List<ProcessInstanceStep> instancesSteps = [SELECT Id,Comments,ProcessInstanceId FROM ProcessInstanceStep WHERE ProcessInstanceId IN:processId];
        System.debug('++instancesSteps' +instancesSteps);
        Map<Id,String> DocProcessStepMap = new Map<Id,String>();
        for(ProcessInstanceStep pisObj:instancesSteps){
            System.debug('+++processid'+pisObj.ProcessInstanceId);
            System.debug('+++processid'+pisObj.Comments);
            DocProcessStepMap.put (pisObj.ProcessInstanceId, pisObj.Comments); 
        }
        System.Debug ('** mapa2: '+DocProcessStepMap);
        for (ASKWA_Documents__c docObj: doclist){
            //if (l.X2nd_Level_Approved__c == 'N'){
            System.debug ('** razon2: '+ DocProcessStepMap.get(DocProcessMap.get(docObj.Id)));
            docObj.X2nd_Level_Remark__c = DocProcessStepMap.get(DocProcessMap.get(docObj.Id));
            //System.debug ('** razon: '+docObj.X2nd_Level_Remark__c);
            //}
        } 
    }
please let mi know what is wrong i will not able to update .
ShirishaShirisha (Salesforce Developers) 
Hi Avinash,

Greetings!

Can you please check the below thread where you can find the sample code to pull the comments and save it in the Case Object field.However,you can replace it with any other custom Object's field.

https://salesforce.stackexchange.com/questions/3850/update-approvers-comment-as-field-update

Also,I would suggest you to try with the below workaround as well which doesnot need any code,if you would like to save it in Custom field on the Approval process itself.

https://developer.salesforce.com/forums/?id=906F00000008kO2IAI

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Claudine Warren 19Claudine Warren 19
You can get this done now with flows.  I follwed this article and it works like a charm.
https://salesforcetime.com/2022/01/26/using-flow-to-save-approval-information-to-record-fields/