2

I have the following in my controller for a visualforce page which is consumed by a PageBlockTable.

I can access other standard fields of the p.ProcessInstance.TargetObject such as p.ProcessInstance.TargetObject.Name and .Id etc...

But how can I access custom fields of TargetObject of where the record is related to? Or is this not possible? If its not possible then I assume I can do it in a seperate query, but I cannot figure out how.

Thank you

List<ProcessInstanceWorkitem> results = [Select p.ProcessInstance.Status, p.ProcessInstance.TargetObject.Name, p.ProcessInstance.TargetObject.Id, p.Actor.Name, p.CreatedBy.Name, p.CreatedDate FROM ProcessInstanceWorkitem p];
realtek
  • 831
  • 4
  • 16
  • 36

1 Answers1

5

You have to perform an additional SOQL query on the TargetObject, as ProcessInstance is polymorphic. for example.

List<ProcessInstanceWorkitem> results = [Select p.ProcessInstance.Status,     p.ProcessInstance.TargetObject.Name, p.ProcessInstance.TargetObject.Id, p.Actor.Name, p.CreatedBy.Name, p.CreatedDate FROM ProcessInstanceWorkitem p];

List<Id> targetObjectIds = new List<Id>();
for(SObject result : results)
{
  targetObjectIds.add(result.p.ProcessInstance.TargetObject.Id);
 }
List<SObject> targetObjectFields = [SELECT Id, Field names FROM SObject WHERE Id IN:targetObjectIds];

Edited: Please change SObject to the object you are currently using.

CAH
  • 69
  • 3
  • Hi, Thats great thank you very much! I seem to get this error though: Compile Error: sObject type 'SObject' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names. at line 38 column 36 – realtek Nov 09 '16 at 15:36
  • Any idea's Chris? Thanks – realtek Nov 09 '16 at 17:28
  • Chris' example is generic, you need to make your query more specific eg if your related object is account replace the ``List targetObjectFields = [SELECT Id, Field names FROM SObject WHERE Id IN:targetObjectIds];`` with ``List targetObjectFields = [SELECT Id, MyField__c FROM Account WHERE Id IN:targetObjectIds];`` assuming your custom field you want is called MyField__c – thegogz Nov 10 '16 at 13:18