In addition to Andrew's answer I'd like to add a sample of SOQL using the TypeOf
.
I faced the same issue time ago when I was trying to get different fields from Task's Parent depending of the parent type. In my case the parents were custom object.
Sample:
SELECT
TYPEOF What
WHEN ObjectA__c THEN fieldA__c, fieldB__c
WHEN ObjectB__c THEN fieldC__c
ELSE Name
END
FROM Task
This is other standard sample using Object.Type:
SELECT
TYPEOF What
WHEN Opportunity THEN Name, amount
WHEN Account THEN Name, AnnualRevenue
END
FROM Task
WHERE
ELSE Name
END
FROMWhat.Type TaskIN ('Opportunity','Account')