In salesforce there is concept called polymorphic fields. Its a lookup relationship type field with more than more one object. Unlike Lookup and Master-detail relationships which are related to a single object, Polymorphic keys can refer to multiple objects. This article is based on polymorphic fields where we will discuss following topics.
Scenario :
- How to get all lookup objects present of Polymorphic field.
- Find all objects present on Related To (WhatId) field of Task Object
- Find all objects present on WhoId field of Task Object
- working on polymorphic lookup field in salesforce
Objects present on Task->WhatId
Get all Objects from Task->WhatId
Map < String, Schema.SObjectType > describeResult = Schema.getGlobalDescribe();
Map < String, String > mapOfObjects = new Map < String, String > ();
Schema.DescribeFieldResult describeField;
for (Schema.SObjectField strFld: describeResult.get('Task').getDescribe().fields.getMap().Values()) {
describeField = strFld.getDescribe();
if (describeField.getType() == Schema.DisplayType.REFERENCE && describeField.getName() == 'WhatId') {
for (Schema.SObjectType reference: describeField.getReferenceTo())
mapOfObjects.put(reference.getDescribe().getName(), reference.getDescribe().getLabel());
}
}
System.debug('mapOfObjects ' + mapOfObjects);
Objects present on Task->Who
Get all Objects from Task->WhoId
Map < String, Schema.SObjectType > describeResult = Schema.getGlobalDescribe();
Map < String, String > mapOfObjects = new Map < String, String > ();
Schema.DescribeFieldResult describeField;
for (Schema.SObjectField strFld: describeResult.get('Task').getDescribe().fields.getMap().Values()) {
describeField = strFld.getDescribe();
if (describeField.getType() == Schema.DisplayType.REFERENCE && describeField.getName() == 'WhoId') {
for (Schema.SObjectType reference: describeField.getReferenceTo())
mapOfObjects.put(reference.getDescribe().getName(), reference.getDescribe().getLabel());
}
}
System.debug('mapOfObjects ' + mapOfObjects);
What is a polymorphic field?
A polymorphic field is a lookup that can point to more than one object. On Task, WhoId can point to a Contact or a Lead, and WhatId can point to Account, Opportunity, Case and many other objects. This is why you cannot write a simple parent relationship query for them.
How the code works
The code loops through all fields of Task and, when it finds a reference field called WhatId, calls getReferenceTo(). That method returns the list of objects the field can point to. Each object name and label is stored in a Map and printed.
Shorter version
Schema.DescribeFieldResult f = Task.WhatId.getDescribe();
for (Schema.SObjectType t : f.getReferenceTo()) {
System.debug(t.getDescribe().getName() + ' - ' + t.getDescribe().getLabel());
} Using the field token Task.WhatId removes the need to loop over every field. Replace it with Task.WhoId to list the objects behind WhoId.
FAQ
How can I check whether a field is polymorphic?
Call getDescribe().getReferenceTo().size(). If it is more than 1, the field points to several objects.
Which other standard fields are polymorphic?
OwnerId on some objects (User or Queue), and fields such as WhatId on Event are common examples.