0

I'm trying to create this in an LWC and can't put together the right SOQL to grab all the data: Approval History

SELECT CreatedDate, StepStatus, OriginalActor.Name, Actor.Name, Comments from ProcessInstanceStep where ProcessInstance.TargetObjectId = :accountId

gets me just about everything except the first Step Name column.

I see there's a ProcessNodeName field in ProcessInstanceNode that seems to have what I'm after but I don't know how to join the two object together. I'm not sure if this is where I should be looking, however.

0

Using a sub-query on ProcessInstance to find the related Steps should do the trick.

SELECT Status,
    (
    SELECT CreatedDate, StepStatus, OriginalActor.Name, Actor.Name, Comments
    FROM Steps
    ORDER BY CreatedDate DESC
    )
FROM ProcessInstance
WHERE TargetObjectId = :accountId

UPDATE

As per comment below a modification to the query in the original attempt to answer.

SELECT Id, LastActor.Name,
    (SELECT Id, Actor.Name, StepStatus, Comments FROM Steps),
    (SELECT Id, ProcessNodeName from Nodes)
FROM ProcessInstance
WHERE TargetObjectId = :accountId
ORDER BY CreatedDate DESC
  • Step Name: Taken as ProcessNodeName from a child object of ProcessInstance called Nodes
  • Date: Taken as CreatedDate of ProcessInstance
  • Status: Taken as StepStatus from a child object of ProcessInstance called Steps
  • Assigned To: Taken as Actor.Name from a child object of ProcessInstance called Steps
  • Actual Approver: Taken as LastActor.Name of ProcessInstance
  • Comments: Taken as Comments from a child object of ProcessInstance called Steps
3
  • It appears this is essentially the same as the query in my question except it uses a subquery on ProcessInstance. I'm looking for the step name in particular. – d2013 May 5 at 13:48
  • @d2013 try the updated code. They don't make the relationships easy between the objects holding those fancy related list line items!! – TSmith May 5 at 14:59
  • 1
    It's still not clear which Node belongs to which Step in this case since there are no IDs linking them together. I may be able to approximate it by ordering each subquery by CreatedDate perhaps and using the array indices. I agree that this object structure seems unnecessarily complicated, especially since I can't use ProcessInstanceHistory which is what the screenshot in my question seems to use. – d2013 May 5 at 15:26
0

After some inspiration from this answer, I found that the ProcessInstanceHistory object can be accessed via the StepsAndWorkItems relationship on ProcessInstance.

I did need to assume the first step in the process is named "Approval Request Submitted" by default since that entry seems to have no ProcessNode.Name, however.

My Apex method ended up looking like this:

public List<ProcessInstanceHistory> getApprovalHistoryByAccountId(Id account) {
    ProcessInstance pi = [
        SELECT Id, (SELECT Id, ProcessNode.Name, CreatedDate,
                     StepStatus, OriginalActor.Name, Actor.Name, Comments
                     FROM StepsAndWorkitems
                     ORDER BY CreatedDate DESC
                    ) 
        FROM ProcessInstance
        WHERE TargetObjectId = :accountId
        ORDER BY CreatedDate DESC
        LIMIT 1
    ];
    return pi.StepsAndWorkitems;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.