Polymorphic Fields
Working with Polymorphic Fields
A polymorphic field is one where the related object might be one of several different types of objects. For example, the WhoID field of a Task can be a Contact or a Lead. To find out if a field is polymorphic, you can query the sys_tablecolumns and check the IsPolymorphic column. There are two pseudo columns related to each polymorphic field:
- {PolymorphicField}Type Column
- {PolymorphicField}ExternalID Column
The pseudo columns are used to efficiently work with polymorphic fields.
Polymorphic Field Types
{PolymorphicField}type can be used in the criteria to specify the type of object the polymorphic relates to. For example: To query only the tasks where the Owner relationship is of type User, the following query can be used:SELECT * FROM Task WHERE OwnerIDType = 'User'For INSERT/UPDATE/UPSERT statements, the polymorphic fields can be used directly, or instead of having to look up the ID of a specific object type, the {PolymorphicField}ExternalID can be used. For example, when trying to update the WhoID of a task, instead of having to find the ID which belongs to a specific Contact, a contact external ID field can be used as a reference. Let's say the contact has an external ID "ContactEmail__c". Instead of using:
SELECT ID FROM Contact WHERE ContactEmail__c = '[email protected]' UPDATE Task SET WhoID = ID from first queryThe following can be executed:
UPDATE Task SET WhoID='[email protected]', WhoIDType='Contact', WhoIDExternalID = 'ContactEmail__c'This tells the service to set the WhoID value for this task to whichever contact ID has the external ID ContactEmail__c equals to [email protected]. The same applies to INSERT & UPSERT statements.