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
arun kumar.ax887arun kumar.ax887 

How to fetch the field values of ProcessInstance

Hi,

 

    I have a requirement, where in I need to assign the text that is present in Comments field of Steps in ProcessInstance to Approved Comments field of Opportunity.  So, kindly suggest a solution to the above mentioned problems.  

 

 

Trigger:


trigger ApprTrigger on Opportunity (after insert,before update) {
Opportunity [] o = Trigger.new;
ApprProcess.ProcessInst(o);
}

 

 

 

Apex Class: 

 

 

 

public class ApprProcess

{
public static void ProcessInst(Opportunity [] o1)
{
for(Opportunity  o2:o1)
{
ProcessInstance [] op = [SELECT Id,Status,(SELECT Id, StepStatus, Comments FROM Steps)FROM ProcessInstance limit 1];
for (ProcessInstance op1 : op)
{
if(op1.Status == 'Approved')
{
o2.Approved_Comments__c = op1.ProcessInstanceStep.comments;
}
}
}  
}

 

 


 

The reference site that I have used is http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_erd_process.htm

 

 

When I am saving the above class I am getting the following error.

ErrorError: Compile Error: Invalid foreign key relationship: ProcessInstance.ProcessInstanceStep at line 12 column 27

 

 

So, can anyone suggest a solution for the above mentioned problem.

 

 

Regards

 

Arun


 


 

jhurstjhurst

Arun,

 

You are pulling a child object in the original query.  In order to traverse the relationship, you have to build a list of the child steps:

 

 

public class ApprProcess {

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

 This should allow you to save the trigger.

 

Hope this helps

Jay

 

shrutiiiiishrutiiiii
  1. How to query process instance step date? i.e. date and time the process step is approved.
Mr Mehedi HasanMr Mehedi Hasan
@Jhurst, worked. Thanks man!