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 scary. In general, we can describe the word polymorphism as the occurrence of multiple forms of the same thing. But how can we relate this concept to SOQL Queries? Join to learn about Polymorphic SOQL in Salesforce.

What is Polymorphic SOQL in Salesforce?

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, 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, and Task Subject. If the Name field is associated with Lead, then you need to print the task Id, Lead Name, Task Status, and Lead Phone. You must use only one SOQL query to get the results and one for loop to publish 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 the 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 use the instanceof keyword in our if and else if conditions. Instanceof keyword will help us to verify the right side of the keyword to the left of declared type in the expression.

WhoId and WhatId in Salesforce

WhoId – Pretty straightforward. Who refers to people? Like Lead ID and Contact ID. Who is the Polymorphic field? It can refer to lead ID or contact ID.

WhatId – Who refers to object Type. This can be Account Id, Opportunity Id, and custom object. This is also a Polymorphic field.


I hope this post helped you to understand the Polymorphic SOQL in Salesforce.

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 using platform, SOAP, REST web services, Lightning application, and third-party packages. Excellent interpersonal skills, and communication, accustomed to working in dynamic environments

Articles: 4


  1. Good post! The only thing I would suggest is changing one word In the first sentence under Requirement: “If the [Name] field is related to contact then you need to print the….”

    I think “Name” should be changed to “WhoId” in order for it to make sense.

Leave a Reply

Your email address will not be published. Required fields are marked *