This might seem to be a fairly simple question, but I am unable to figure out how to use the fields returned by a Polymorphic SOQL query.
For example: I ran the following query in dev console's query editor:
SELECT TYPEOF WHAT
WHEN OPPORTUNITY THEN Name,StageName
WHEN ACCOUNT THEN Name
ELSE Email
END
FROM EVENT
It shows that I have one record which matches this criteria, but it doesn't show the record details.
Here is the output:
When I run this segment of code in Anonymous window, the list only prints WhatId and the Id of the event itself.
list<Event> newlist=[SELECT TYPEOF WHAT
WHEN OPPORTUNITY THEN StageName
ELSE Email
END
FROM EVENT];
system.debug(newlist);
if(newlist.get(0).What instanceof Opportunity){
//what should I do here to access fields of opportunity?
}
I understand that I can fetch the WhatId from the list and use that to query the fields on Opportunity object. But if I need to run the DML again to get the required fields, what is the use of the polymorphic DML statement?
Any help is appreciated. Many thanks.