Lets understand what is Polymorphic Relationships. In a polymorphic relationship, the referenced object of the relationship can be one of several different types of object for example who and what field on Task and Event Object. Who can be Contact or Lead.
Relationship field and Polymorphic field
Let understand the different between Relationship field and Polymorphic field
Relationship field
|
Polymorphic field
|
OwnerId field of the Account and contact object. Most of the Lookup and master-detail fields. |
OwnerId field of the event Object. It can be Calender or User. Or Who |
Example Select id, Owner.Name from Account |
Example SELECT Id, Owner.Name FROM Event WHERE Owner.Type = 'User' |
To get Parent record data use dot(.). |
You can use a TYPEOF clause in a query |
What Field is also polymorphic field on Event/Task object. What about if we need to get field base on SObject Type? We have solution of this problem : TYPEOF.
TypeOF
TYPEOF is an optional clause that can be used in a SELECT statement of a SOQL query when you’re querying data that contains polymorphic relationships. A TYPEOF expression specifies a set of fields to select that depend on the runtime type of the polymorphic reference. TYPEOF is available in API version 46.0 and later (It is also available in API version 26.0 and later as part of a Developer Preview).
Syntax:
TYPEOF typeOfField
WHEN whenObjectType THEN whenFieldList [...]
ELSE elseFieldList
END
FROM objectType
Use Case:
On Task Object "Related To" (What) is Polymorphic field. Which can be account, opportunity or other sobject. If "Related To" (What) field is related to account then we need to get account phone and number of employees fields. If the "Related To" (WhatId) field is related to Opportunity then we need to get amount and closeDate.
Set see how we can create our query for above requirement.
TYPEOF What
WHEN Account THEN Phone, NumberOfEmployees
WHEN Opportunity THEN Amount, CloseDate
ELSE Name
END
FROM Task
WHERE What.Type In ('Account','Opportunity')
- In this query we used TYPEOF to get runtime type of the polymorphic reference field.
- We can use the TYPE to filter our query.
Let see how to handle it in Apex Class
TYPEOF What
WHEN Account THEN Phone, NumberOfEmployees
WHEN Opportunity THEN Amount, CloseDate
ELSE Name
END
FROM Task
WHERE What.Type In ('Account','Opportunity')];
for (Task tsk: listTask) {
if (tsk.What instanceof Account) {
Account acc = tsk.What;
System.debug('--acc--->'+acc.NumberOfEmployees);
} else if (tsk.What instanceof Opportunity) {
Opportunity opp = tsk.What;
System.debug('--opp--->'+opp.CloseDate);
}
}
- Use InstanceOf to check the Object Type.
Very informative, Thank you.
ReplyDeleteCan we create polymorphic fields and relations on custom objects?
Unfortunately we can't
ReplyDeleteشركة عزل خزانات بجازان
ReplyDeleteشركة عزل فوم بجازان
شركة عزل اسطح بجازان
شركة عزل اسطح بابها
شركة كشف تسربات المياه بجازان
Wow its a great blog.
ReplyDeleteBest Jewellery Wholesaler in Jaipur is Ratnavali. It is a best place to buy silver jewellery Jaipur, Jaipuri gold jewellery, gemstone figures in Jaipur, Rajasthan. We have skilled craftsmanship who made best designer collection of jewellery for you.
Hi, this is good article
ReplyDeleteinternship request letter | Internship completion letter | internship companies | internship resume objective | internship application letter | Internship with training | internship email | internship experience | What internship means | Internship acknowledgement
Thank you for sharing this valuable information.
ReplyDeleteApparrant is the leading UX UI Design agency having experienced designers and developers working throughout the year on important and life changing products. We have been awarded as top UI UX Design agency in India by multiple agencies. If you are looking for best UX Design Agency in Noida, UI UX Design Agency in Faridabad, and UX UI Design Company in Delhi then Apparrant is best for you.
Brother, your content is awesome and very informative. I also have a website , please visit and share you review too
ReplyDeleteBrahmrishi is very good website
jaisana
Thanks for sharing this information.
ReplyDeleteYes Done is the best online site for on demand home services, professional service providers in Jaipur, Rajasthan. We are providing top services like carpenter, cleaning, TV, best pest control services in Jaipur, ro repair service in Jaipur, ac repair service in Jaipur online by Yes Done is there for services at your doorstep in Jaipur, Rajasthan. For more service visit our website. For more services visit our website.
Hey Nice Blog!!! Thank you for sharing information. Wonderful blog & good post.Its really helpful for me, waiting for a more new post. Keep Blogging!!!
ReplyDeleteSSB Coaching in Lucknow | Shield Defence Academy
Best coaching for NDA 2021 in Lucknow | Shield Defence Academy
Congratulations, your writing is really good.
ReplyDeleteRead about Wi4 which is Healthcare Data Analysis Software Development and Medical Imaging Software Development Company . We are also best Remote Patient Monitoring Software Development and Patient Engagement Solutions Provider agency having experience with IoT Healthcare Software Developer in the USA also.
Buy Viagra super p-force online is commonly used for human to stimulate erection in body. Its known as Sildenafil commonly named Viagra.
ReplyDelete