realtek
realtek

Reputation: 841

Salesforce, accessing TargetObject fields from ProcessInstance object using SOQL

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];

Upvotes: 2

Views: 3432

Answers (1)

CAH
CAH

Reputation: 69

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.

Upvotes: 5

Related Questions