In Salesforce, a polymorphic relationship refers to a type of relationship between objects where the referenced object can be one of several different types.
In Salesforce, a polymorphic relationship refers to a type of relationship between objects where the referenced object can be one of several different types.
For instance, in a Task object, the “Who (Name)” relationship field can be associated with either a Contact or a Lead, allowing for greater flexibility in data modeling.
Who refers To – Person – Lead/Contact
In a Task object, the “What (Related To)” relationship can be related to any custom or standard object except Lead or Contact
Structure
Example #1
Example #2
Example in Apex
Hands-On
Hands-On in Apex
Watch the Complete Videos
Assignment
Create an Apex Class and name is SOQLDemo
Create a method and name it searchAccount that accepts a single parameter as the account name or portion of the account. Make a SOQL Query to get all the accounts whose names are the matching name as parameters and put a debug log.
Create a method polymorphicQueryDemo and make a SOQL query to get the Event Information that is related to Account, Contact, Opportunity, and Case. See more for the query
When What is Account SELECT Id, Name, Phone, Industry
When What is Opportunity SELECT Id, Name, Amount
When What is Case SELECT Id, CaseNumber, Subject, Status
When What is Contact SELECT Id, Name, Email, Phone
create a method soqlForLoopDemo and do the query on Account, Contact, and Opportunity using SOQL For Loop
Create a method that accepts the List of Industries and the keyword. Inside the method make the SOQL Query that filters the account based on Industry and the name based on the parameter.
Note: – Name should be having “Like” the filter on the Account Name field
Create a method and have the below query inside that method
Query All the Contact Record that does not have any Account Related To
Query All the Contact Records that are related to Account Record
Query All the Account Record that does not have any contact
Query All the Account Record that does have min one Contact Record
Query the Invoice Record, its Related Account, and Account Related Parent Account Record
Query the Invoice Line Item Record, its Related Invoice, Invoice Related Account, and Account Related Parent Account Record
Write a SOQL Query to Show the MAX amount from the opportunity
Write a SOQL Query to identify the duplicate records by Name & Email
Write a SOQL Query to display the count of Accounts for every Industry
Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion