[SalesForce] Why can some tables be accessed in a subquery, but not in a regular query

I am going through code that does approvals and I see the following query:

Select Id, TargetObjectId, Status,
    (
        Select Id, ProcessInstanceId, ActorId, Actor.Name, StepStatus, Comments 
        From StepsAndWorkItems
        Where 
            StepStatus = 'Pending'  and
            isDeleted = false 
        Order By 
            Createddate Desc
        Limit 1
    )
From ProcessInstance
Where
    isDeleted = false and
    TargetObjectId = 'a0WM0000002F8gdMAC' and
    Status = 'Pending'
Order By 
    Createddate Desc
Limit 1

This works fine. From what I can see, the ProcessInstance object is somehow connected to the StepsAndWorkItems object that's referenced in the subquery. So this assumes that StepsAndWorkItems is an actual object that can be queries. And yet, if I try this:

Select Id, ProcessInstanceId, ActorId, Actor.Name, StepStatus, Comments 
From StepsAndWorkItems
Where 
    StepStatus = 'Pending'  and
    isDeleted = false 
Order By 
    Createddate Desc
Limit 1

I get an error stating: sObject type 'StepsAndWorkItems' is not supported.

Can someone shed some light as to why this is happening?

Best Answer

The query which you are attempting to execute is using a child relationship name, StepsAndWorkItems , which isn't valid in that context.

StepsAndWorkItems is the child relationship name for ProcessInstanceHistory under the ProcessInstance object.

The underlying objects are ProcessInstanceStep and ProcessInstanceWorkItem. You can query those two objects individually but you cannot use the child relationship name to query them both at the same time without doing it from within the parent context.

Related Topic