Polymorphic Relationships in Soql Queries
Prakash Jada

Prakash Jada

5+ years of experience in all phases of SDLC (Analysis, Design, Development, Administration, Testing, Implementation, and Support) in software Applications using Salesforce (CRM). Well versed in Cloud Technology and on-premise infrastructure integration for Salesforce.com using Force.com platform, SOAP, REST web services, Lightning application, and third-party packages. Excellent interpersonal skills, and communication, accustomed to working in dynamic environments

Working with Polymorphic Relationships in SOQL Queries

I know so many of us are scared about the word polymorphism but do not worry it is not that much scary. In general words we can describe the word polymorphism means occurrence of multiple forms by a same thing. But how can we relate concept this to SOQL Queries?


First, can we use a lookup field to store the information related to 2 different objects? Is it possible in Salesforce?

Yes, absolutely it is possible if you do not believe me then please navigate your object manager and select Task object and select the fields and relationships. If you examine it the lookup field is linked to multiple objects.

Label: Name
API Name: WhoId
Related to: Contact, Lead


If the Name field is related to contact then you need to print the task Id, contact Name, Task Status, Task Subject. If the Name field is related to Lead then you need to print the task Id, Lead Name, Task Status, Lead Phone. You must use only one SOQL query to get the results one for loop to print the results.

Can we do it? Is it possible? Let us see how we can do it. Let’s try to build the query first before writing some actual logic.


SELECT Id, Status, Subject, TYPEOF who
                        WHEN Contact THEN Name
                        WHEN Lead THEN Name, Phone

If you look at the query, I used a keyword “TYPEOF” which is used to get the type of value based on field that we are using. Here we are using “TYPEOF Who”, This means based on whoId if it is related to contact then we are getting the Name or If the whoId is related to Lead then we are getting the Name and Phone. So simple right.


// List to fetch the Task records based on WhoId
List<Task> taskList = [SELECT Id, Status, Subject, TYPEOF who
							WHEN Contact THEN Name
							WHEN Lead THEN Name, Phone
FROM Task];

// Loop to iterate over the query results
for(Task task : taskList) {
	if(task.Who instanceof Contact) {
		Contact con = task.Who;
		System.debug('Task Id: ' +task.Id+ 'related to Contact Name' +con.Name);
	}else if(task.who instanceof Lead) {
		Lead lead = task.who;
		System.debug('Task Id ' + task.Id +' related to Lead Name ' + lead.Name);

Here we are using the instanceof keyword in our if and else if conditions. Instanceof key word will help us to verify the right side of the keyword to the left of declared type in the expression.

Share this article


Leave a reply

Keep in Touch

Subscribe for Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,229 other subscribers


Our Supporter


Apex Hours

Apex Hours is one stop platform to learn Salesforce skills and technology

Join our Newsletter and get tips and tricks how to explore the salesforce for free!