Home » All Blogs » Working with Polymorphic Relationships in SOQL Queries

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?

DON’T WORRY THIS DOCUMENT WILL HELP YOU TO UNDERSTAND HOW WE CAN DO THAT!!!!

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.

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

Requirement

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.

SOQL

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

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.

Example:

// 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
							END
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 your love:
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

Articles: 3

3 Comments

  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 *