1

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 '16 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 '16 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 '16 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 '16 at 15:14
0

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; 

    myWrappers.add(wrap);

  }

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

}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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