+ Start a Discussion
SFDC n12SFDC n12 

Approval comments from approval process to a custom field

Hi,

I am having a approval process which is having 4 stages of approver

i want to capture the comments enterd by the 3 level of approver alone (not any other apperover) in a custom  field 

Help me how to achieve it

Thanks in Advance
Tushar sharmaTushar sharma
you can use PocessInstance for capture comment 
SELECT Id, (SELECT Id, StepStatus, Comments FROM Steps)
FROM ProcessInstance

more info about processinstance can be found here  https://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_processinstance.htm
SFDC n12SFDC n12
I have written the following controller, please let me know if its right


public class ApprProcess {
    public static void ProcessInst(AccountExceptions__c [] o1) {
        for(AccountExceptions__c o2:o1) {
           
             List<AccountExceptions__c > op= [SELECT Id, ADR_Comments__c, (Select Id, IsPending, ProcessInstanceId, TargetObjectId, StepStatus, OriginalActorId, ActorId, RemindersSent, Comments, IsDeleted, CreatedDate, CreatedById, SystemModstamp From ProcessSteps)
                                      FROM AccountExceptions__c];                       
                                     
            for (AccountExceptions__c  op1 : op) {
                for (ProcessInstanceHistory pis : op1.ProcessSteps) {
                  
                        o2.ADR_Comments__c = pis.Comments;
                   
                }
            }
        }  
    } 
}
Tushar sharmaTushar sharma
Do you get any error in this if yes please mention here. one friendly suggestion Never write SOQL in for loop otherwise you hit the limit. your code looks fine for me
SFDC n12SFDC n12
i have changed my trigger to your query to populate my custom field with the comments that i enter, its not populating 


Here is my contrioller

public class ApprProcess {
    public static void ProcessInst(AccountExceptions__c [] o1) {
        for(AccountExceptions__c o2:o1) {
            ProcessInstance [] op = [SELECT Id, (SELECT Id, StepStatus, Comments FROM Steps)
FROM ProcessInstance];
            
            for (ProcessInstance op1 : op) {
                for (ProcessInstanceStep pis : op1.Steps) {
                    if(op1.Status == 'Approved') {
                        o2.ADR_Comments__c = pis.Comments;
                    }
                }
            }
        }  
    } 
}

 
SFDC n12SFDC n12
i didnt get any error in both the cases but its not populating the comments in my ADR COMMENTS_C text field , Help me what changes need to make in my class
Tushar sharmaTushar sharma
first check in debug that do you getting any result 
ProcessInstance [] op = [SELECT Id, (SELECT Id, StepStatus, Comments FROM Steps)
FROM ProcessInstance];

in this SOQL
SFDC n12SFDC n12
i checked , it didnt return any result