soql – What is the easiest way to get a Task WhatId parent’s RecordType?

I need to query a Task’s related to (i.e. WhatId) parent’s recordtype

  • The Task What relationship is polymorphic and hence its WhatId could point at Account, Opportunity, myCustomObject__c, etc. These objects might or might not have record types.

I looked at the SOQL reference guide on polymorphic querying and I see some options that aren’t really practical

  • SELECT Id, What.Type FROM Task // just tells me SObjectType of the parent
  • SELECT Id, What.Name FROM Task // returns the parent’s Name field

and

SELECT
TYPEOF What
 WHEN Account THEN Id, RecordType.Name
 WHEN Opportunity THEN Id, RecordType.Name
END
FROM Task

but this isn’t scalable as new SObjects are added to the org

Read more here: Source link