1

I am attempting to retrieve the current process instance step name based on the processInstance step but cannot figure out how to relate this to the processDefintion / ProcessNode.

My understanding is that the processNode Describes a step in a process definition while the processInstanceStep is an instance of this ProcessNode Definition. Can identify the latest processInstance for my custom object and can find the associated steps of that process. Using this query

set<Id> processInstanceIds = new set<Id>();

            for (ProcessInstance pi : [ SELECT TargetObjectId, Status, CreatedDate,
                                            (
                                            SELECT Id, ProcessInstanceId, StepStatus, Comments, CreatedDate
                                            FROM Steps
                                            ORDER BY CreatedDate DESC
                                            )
                                            FROM ProcessInstance
                                            WHERE TargetObjectId IN: wireRequestIds
                                            ORDER BY CreatedDate DESC
                                        ])

What I need is now based on the latest step to get the step name..

ProcessInstanceStep:{ProcessInstanceId=04g4B0000006fFhQAI, Id=04h4B00000070O7QAI, StepStatus=Started, CreatedDate=2016-06-06 23:24:28}


ProcessInstance:{TargetObjectId=a4S4B000000CaRCUA0, Status=Pending, CreatedDate=2016-06-06 23:24:28, Id=04g4B0000006fFhQAI}

This leads me to the ProcessInstanceNode.

But I cannot get this query to return anything..

   list<ProcessInstanceNode> nodes = [SELECT Id, NodeStatus, ProcessNodeName, ProcessInstanceId 
                                           FROM processinstancenode 
                                           WHERE processinstanceid IN: processInstanceIds 
                                           AND nodeStatus = 'Pending' ];

Anyone done this before can send me in right direction??

2 Answers 2

2

This seems to meet my need..

@Future
public static void recordTypeSetting(set<Id> parentObjectIds )
{
    if (!parentObjectIds.isEmpty())
    {
        map<Id, ProcessInstance> latestInstance = new map<Id, ProcessInstance>();

        for (ProcessInstance pi : [ SELECT Id, TargetObjectId, Status, CreatedDate,
                                        (
                                        SELECT Id, ProcessInstanceId, StepStatus, Comments, CreatedDate
                                        FROM Steps
                                        ORDER BY CreatedDate DESC
                                        )
                                        FROM ProcessInstance
                                        WHERE TargetObjectId IN: parentObjectIds
                                        ORDER BY CreatedDate DESC
                                    ])
        {
            system.debug('processInstance: ' + pi);
            system.debug('steps.size: ' + pi.Steps.Size());

            if (!latestInstance.containsKey(pi.TargetObjectId))
            {
                latestInstance.put(pi.TargetObjectId, pi);
            }
            else
            {
                ProcessInstance maxInstance = latestInstance.get(pi.TargetObjectId);
                if ( maxInstance.CreatedDate < pi.CreatedDate  )
                {
                    latestInstance.put(pi.TargetObjectId, pi);
                }
            }
        }

        set<Id> processInstanceIds = new set<Id>();

        for ( ProcessInstance pi : latestInstance.values())
        {
            for ( ProcessInstanceStep pis : pi.Steps)
            {
                if ( pis.StepStatus == 'Started')
                {
                    processInstanceIds.add( pi.Id );        
                }

            }
        }

        map<Id, ProcessInstanceNode> instanceNodeMap = new map<Id, ProcessInstanceNode>();

        for( ProcessInstanceNode node : [SELECT Id, NodeStatus, ProcessNodeName, ProcessInstanceId 
                                           FROM ProcessInstanceNode 
                                           WHERE ProcessInstanceId IN: processInstanceIds
                                           AND NodeStatus = 'Pending' ])
        {
            instanceNodeMap.put(node.ProcessInstanceId, node);
        }

        for ( Id parentObjectId : parentObjectIds )  
        {
            system.debug('Parent Object Id: ' + parentObjectId);
            ProcessInstance pi = latestInstance.get( parentObjectId );
            system.debug('process Instance: ' + pi );
            system.debug('current Active Step Name: ' + instanceNodeMap.get( pi.Id ).ProcessNodeName );
        } 

    }
}
1
  • yes query will return the ProcessInstanceNode in async process
    – Ratan Paul
    Commented Jun 7, 2016 at 5:55
0

You can achieved all of them with a single query.

List<ProcessInstance> processInstances = 
    [SELECT Id, Status,
     (SELECT Id, StepStatus, Actor.Name, OriginalActor.Name, ProcessNode.Name, 
       Comments, CreatedDate FROM StepsAndWorkitems ORDER BY CreatedDate DESC)
FROM ProcessInstance WHERE TargetObjectId IN: parentObjectIds ORDER BY CreatedDate DESC];
2
  • The challenge here was to update a field on the approval record called Current_process_step__c with the step name of the active approval process. This step name was then used to evaluate additional criteria. The issue here was triggering this @ future code and having the transaction finish. Due to order of operations you cannot do this during the transaction as the approval isn't written to the database during the transaction hence the @ future. The query you listed will return all process instances and does not return the 'current step' for the approval process.. Commented Sep 23, 2022 at 9:08
  • Hence this is why I build the map and only retain the latest process instance. The query above will return all processes and not necessarily the active one.. nor will it find the latest process instance for the active process.. I do appreciate your response @Ripon Commented Sep 23, 2022 at 9:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .