Development questions
Write a SOQL query using TYPEOF on Task to select Lead.Company if Who is a Lead, and Contact.Department if Who is a Contact.
TYPEOF Who returns different fields depending on whether the related record is a Lead or a Contact.
soql
SELECT Id, Subject,
TYPEOF Who
WHEN Lead THEN Company
WHEN Contact THEN Department
END
FROM Task
In Apex, check the type with task.Who instanceof Lead and cast to read the fields.
soqlscenariopolymorphictypeof
More Development questions
- What is an Apex Trigger in Salesforce and when should you use it over Flows?
- What is the difference between before triggers and after triggers?
- Explain all Trigger context variables (isInsert, isUpdate, isDelete, isUndelete, isBefore, isAfter, isExecuting, size, operationType).
- What is the difference between Trigger.new and Trigger.old?
- In which trigger events are Trigger.new and Trigger.newMap available?
- In which trigger events are Trigger.old and Trigger.oldMap available?