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


I have written the following controller, but its not updating my field ADR COMMENTS__C with the comments


public class ApprProcess {
    public static void ProcessInst(AccountExceptions__c [] o1) {
        for(AccountExceptions__c o2:o1) {
            ProcessInstance [] op = [SELECT Id, (SELECT Id, StepStatus, Comments FROM StepsAndWorkitems)
FROM ProcessInstance];
        

            for (ProcessInstance op1 : op) {
                for (ProcessInstanceStep pis : op1.Steps) {
                    if(op1.Status == 'Approved') {
                        o2.ADR_Comments__c = pis.Comments;
                        
 
                    }
                }
            }
        }  
    } 
}


Help me how to achieve it

Thanks in Advance
 
SonamSonam (Salesforce Developers) 
I suppose you will also have to request for the following fields from the Steps object(nested) to be able to assign its value to another field:


SELECT Id, (SELECT Id, StepStatus, Comments FROM Steps) FROM ProcessInstance
 in your code here:

 for(AccountExceptions__c o2:o1) {
            ProcessInstance [] op = [SELECT Id, (SELECT Id, StepStatus, Comments FROM StepsAndWorkitems)
FROM ProcessInstance];
SFDC n12SFDC n12
I didnt get you , so should i include this query



SELECT Id, (SELECT Id, StepStatus, Comments FROM Steps) FROM ProcessInstance in my controller
 
SonamSonam (Salesforce Developers) 
Try editing the SOQL in your controller:
for(AccountExceptions__c o2:o1) {
            ProcessInstance [] op = [SELECT Id, (SELECT Id, StepStatus, Comments FROM Steps)
FROM ProcessInstance];

as I think the value of comments field might not be coming thru.
 
SFDC n12SFDC n12
yes the comments are not getting fetched ,

i am just using the controller , i need to call the controller in a trigger to update t he value right ?


just the controller without used in a trigger wont update the field isint ?

 
SonamSonam (Salesforce Developers) 
Yes, you will have to trigger this class some how through a trigger or vf page suh that this gets the value of comment.
SFDC n12SFDC n12
i have an existing trigger on my custom object , i am trying to call my controller after insert like this

      ApprProcess process = new ApprProcess();
      process.ProcessInst();


but i am getting this error

Error: Compile Error: Method does not exist or incorrect signature: [ApprProcess].ProcessInst() at line 88 column 7    


do i need to pass any parameters in 


     process.ProcessInst();

if so what parameters should i call
SonamSonam (Salesforce Developers) 
Yes, you need to pass parameter as you have defined in the method in the class:
   public static void ProcessInst(AccountExceptions__c [] o1)

You should be passing an array of  AccountExceptions__c records which you have in the apporval process.