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.

DevelopmentHardSalesforceDeloittePwC

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