2

How come I can compile a query on workitem to return processinstanceID but the related targetobjectid (and then of course the targetobjectid.name ) return the error:

Edit - sorry I wasn't clear but ultimately I want the targetobject's name field - to display to users.

ProcessInstance.TargetObjectId.name brings " Didn't understand relationship 'TargetObjectId' in field path.... type error"

Error: Compile Error: Didn't understand relationship 'ProcessInstanceId' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 15 column 50

public class UsersItemsToApprove {

public UsersItemsToApprove() {

}


public UsersItemsToApprove (ApexPages.StandardController controller) {

}


   public List<ProcessInstanceWorkitem> getWorkItems() {
    List<ProcessInstanceWorkitem> results = new List<ProcessInstanceWorkitem>();
    List<ProcessInstanceWorkitem> allitems = [SELECT ElapsedTimeInDays, 
           ProcessInstanceId,  ProcessInstanceId.targetobjectid
            FROM ProcessInstanceWorkitem
            WHERE ActorId= :UserInfo.getUserId()];


    for(ProcessInstanceWorkitem thisitem : allitems)



    {

            results.add(thisitem);

    }
    return results;
}



}
1
  • 1
    Try ProcessInstance.targetobjectid FROM Commented Oct 31, 2016 at 19:01

3 Answers 3

1

You need to use the relationship name when getting cross-object fields (a Right Outer Join). You can get this value using:

system.debug(ProcessInstanceWorkItem.ProcessInstanceId.getDescribe().getRelationshipName());

The above yields ProcessInstance, so you should do:

SELECT ProcessInstance.TargetObjectId FROM ProcessInstanceWorkItem

You have to continue this process on down the line. Only the final cross-object field will end up being unmodified. So if you want to go a third level, you would use:

SELECT ProcessInstance.TargetObject.Name FROM ProcessInstanceWorkItem
3
  • Ok, thanks. But ProcessInstance.TargetObjectId.name brings back the " Didn't understand relationship 'TargetObjectId' in field path."
    – jaw999
    Commented Oct 31, 2016 at 19:53
  • Works for me...did you try to add more fields after? Like ProcessInstance.TargetObjectId.Name? You would have to also use the relationship name for TargetObjectId, which would be TargetObject.
    – Adrian Larson
    Commented Oct 31, 2016 at 19:55
  • ah, that's it, again i was having the id in there.... feeling slow today I guess. thanks
    – jaw999
    Commented Oct 31, 2016 at 19:56
2

Just to complement what was already answered:

When querying attributes of a related (lookup) object, you should access:

  • ProcessInstance.Name for standard lookups (your case);
  • Custom_lookup__r.Attribute__c for custom object lookups;
1

Try this, this should work

List<ProcessInstanceWorkitem> allitems = [SELECT ElapsedTimeInDays, 
           ProcessInstanceId,ProcessInstance.TargetObjectId
            FROM ProcessInstanceWorkitem
            WHERE ActorId= :UserInfo.getUserId()];

You must log in to answer this question.

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