ForceChallenges
ProblemsDailyNotesInterview
Sign in

Practice

All problemsSOQL practiceApex trigger challengesDaily challengeOpportunityTrigger.oldMapBefore UpdateBulkificationContactValidationGROUP BYSemi-JoinAccountBefore Insert

Interview questions

All questionsAdminAdministrationApexApex TriggerAsync ApexDevelopmentField ServiceField ServiceIntegrationLwc

By company

DeloitteSalesforceAccentureInfosysPwCCognizant

Learn

Study notes

ForceChallenges

AboutContact usPrivacy PolicyTerms of ServiceFor AI assistants (llms.txt)
© 2026 ForceChallenges · Salesforce SOQL & Apex trigger practice
Soql questions13 of 30
SoqlHard 1 min read

How do you query Polymorphic lookup fields like What and Who on Task/Event or Owner using TYPEOF in SOQL?

Asked atSalesforceDeloitteSlalom

Answer

TYPEOF lets you select different fields depending on the actual object type of a polymorphic relationship (What, Who on Task/Event, Owner, etc.).

soql
SELECT Id, Subject,
  TYPEOF Who
    WHEN Lead    THEN Company, Email
    WHEN Contact THEN Account.Name, Department
    ELSE Name
  END
FROM Task

You can also filter polymorphic fields with Who.Type = 'Lead'.

soqlpolymorphictypeof
Practice SOQL & Apex
PreviousHow does FIELDS(ALL), FIELDS(STANDARD), and FIELDS(CUSTOM) work in SOQL, and what are their limitations in Apex?Next How do INCLUDES and EXCLUDES operators work when querying Multi-Select Picklist fields in SOQL?

More Soql questions

  • How do you query Custom Metadata Types (getAll(), getInstance()) without consuming SOQL governor limits?
  • How do you query Big Objects using SOQL vs Async SOQL, and why must you query indexed fields in exact order?
  • What is the governor limit for Aggregate queries and how do rows counted in AggregateResult contribute to the 50,000-row limit?
  • What are the Governor Limits for SOQL (100 synchronous queries, 200 asynchronous queries, 50,000 total rows retrieved)?
  • Which fields are indexed by default in Salesforce, and what makes a SOQL filter non-selective (e.g., !=, NOT LIKE '%abc', leading wildcards, formula fields)?