0

I'm trying to figure out how to pull a field 'Industry__c' from the What lookup field on the Task Object.

Right now I've tried only pulling it when the type would be an object that would have the field, and just pulling it normally but neither will work.

I need to pull this through a SOQL query to display in a table along with it's related task in the same row, as well as other Tasks shown in a similar manner on a visualforce page

1
  • Just add a second query.
    – Adrian Larson
    Commented Sep 15, 2017 at 20:40

2 Answers 2

1

You have to use below approach to query the field of the parent object related to the Task.

Task t = [SELECT WhatId FROM Task WHERE Id = 'xxx'];
My_Object__c m = [SELECT FieldName__c FROM My_Object__c WHERE Id =: t.WhatId];

Reference:

How do I access custom fields of Task parent record using SOQL?

0

Here's a somewhat hacky possible idea. I tried it with Asset as the example object and it seems to work. Unfortunately it relies on the fact that an object's 3-character prefix is indicative of the object type, so it's not stable for change set deployment unless the object already exists with the same prefix in both orgs.

Create a custom lookup on Activity, not included in page layouts, for the specific object whose data you want to retrieve from its related tasks. For example Asset__c.

Now create a Process Builder to execute whenever a Task is created/updated. Action criteria:

[Task].WhatId != [Task].Asset__c && 
OR(LEFT([Task].WhatId, 3) = '02i', ISBLANK([Task].WhatId))

The first line ensures the rule only fires when WhatId is not the same as Asset__c, and the second line ensures it also only fires when the WhatId is an Asset or blanked out.

Then add an Update Field action which, under these conditions, assigns the value of WhatId to Asset__c. Process Builder will allow you to do this!

I tested by 1) adding a Task to an Account. No error was fired. Then 2) I added a Task to an Asset. I can confirm the ID got copied to Asset__c.

For existing Tasks you'd want to do a one-time backfill. You could accomplish this in Data Loader by exporting Id, WhatId for all the tasks with a WhatId matching the right prefix and then running the CSV back through in Update mode to fill the Asset__c field.

Once you've gone to all this trouble, what's the difference? Well, now Asset__c is a standard lookup rather than polymorphic. So you can query any field you want from the relationship. :) You could even add a custom lookup for each type of object you want to do this with, and then have one Process Builder decide which field to fill if any.

You must log in to answer this question.

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