I am trying to query the ProcessInstanceWorkitem object which has a relationship to the ProcessInstance object, however the ProcessInstance object has a polymorphic relationship by using the TargetObject field.

The question is, with my query below - I understand I can either use separate queries to get to those custom fields in whatever object relates to them or you could use TYPEOF (although that does not seem to be enabled or possible yet).

So I am left with option 1 of having a separate query, the issue is I don't quite understand how I can do a second query of this related object and get to the custom fields and them combine them into one List so I can display in a PageBlockTable?

Here is my query:

results = [Select p.ProcessInstance.Status, 
           p.ProcessInstance.TargetObject.Name, p.Actor.Name, p.CreatedBy.Name, 
           p.CreatedDate FROM ProcessInstanceWorkitem p WHERE 
           (CALENDAR_MONTH(p.CreatedDate) = : dropdownMonth AND 
           CALENDAR_YEAR(p.CreatedDate) = :dropdownYear)];

When performing a second query, the issue is these results need combining so I can just display the additional fields from the other object I am selecting the fields from.

  • Why are you finding it difficult to construct a second query? Do the process instance results span multiple object types for the target object ID?
    – Eric
    Nov 10, 2016 at 12:44
  • Yes they will span in the future but not just yet. Either way, I need to combine those results into the same List so I can display it in the same table on a PageBlockTable as additional columns?
    – realtek
    Nov 10, 2016 at 13:09
  • You will have to use Describe methods and generic sObjects and multiple queries. If the fields you want to display are common across all the objects then you can build an Apex representation of the objects (class) to collect and display the information in a single page block table. If the fields are not common then you need to rethink your UI design to accommodate multiple sObject types
    – Eric
    Nov 10, 2016 at 14:51
  • Thanks, I have got this working now with a Wrapper class. I will post a solution soon. Cheers all!
    – realtek
    Nov 10, 2016 at 15:14

1 Answer 1


Resolved by creating a wrapper class.

Solution found here on Stackexchange

I then checked to see the specific type of object and manually queried each one.

public Class MyController {

  // this is used for the data table
  public List<MyWrapper> myWrappers {get; set}

  // query and join your objects in the constructor
  public MyController() {

    myWrappers = new List<MyWrapper>();

    MyObject1__c o1 = [SELECT.......];  
    MyObject2__c o2 = [SELECT.......]; 

    MyWrapper wrap = new MyWrapper()
    wrap.obj1 = o1;
    wrap.obj2 = o2; 



  public Class MyWrapper {
    public MyObject1__c obj1 {get; set}
    public MyObject2__c obj2 {get; set}


You must log in to answer this question.

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