Understanding Relationship Fields and Polymorphic Fields
In a polymorphic relationship, the referenced object of the relationship can be one of several different types of object.
Here’s an overview of the terminology used in this document.
- Name as a label on the Salesforce UI of certain types of objects, such as Tasks. Represents the related person, usually a Lead or Contact.
- WhoId: The corresponding API name of the Name. Used in code or in API calls.
- Related To as a label on the Salesforce UI of certain types of objects, such as Tasks. Represents the related object, usually an Account. Not a person object.
- WhatId: The corresponding API name of the Related To.
- Who: The value of
relationshipNameof the Name. Used in queries as a stand-in for Name. - What: The value of
relationshipNameof the Related To. Used in queries as a stand-in for Related To.
Some fields are relationship fields, which means they can be used to get information about a related object. And some of those relationship fields are polymorphic fields. A polymorphic field is one where the related object might be one of several different types of objects. For example, the Who relationship field of a Task can be a Contact or a Lead.
To determine what kind a field is, call describeSObjects() on the object and examine the properties for the field.
- If
relationshipNameis not null, the field is a relationship field. - If, in addition,
namePointingis true,polymorphicForeignKeyis true, andreferenceTohas more than one referenced object type, then the field is polymorphic.
Example of a Relationship Field
The OwnerId field of the Account object has the following properties:
relationshipName= OwnernamePointing= falsereferenceTo= User
This means it is a relationship field. Use the relationshipName as a pseudo field to get information about the related object, whose type is specified in the referenceTo field. For example, you can use the following SOQL query:
Use the name of the relationship (Owner) not the name of the field (OwnerId).
Example of a Polymorphic Field
The OwnerId field of the Event object has the following properties:
relationshipName= OwnernamePointing= truepolymorphicForeignKey= truereferenceTo= Calendar, User
This means it is a polymorphic field. Owner could be a Calendar, or a User. For example, you can use the following SOQL query:
You can use polymorphic fields several ways.
- You can use the polymorphic field for the relationship.
- You can use the
Typequalifier on a polymorphic field. - You can use a
TYPEOFclause in a query.
You can also combine these techniques for complex queries.
Common polymorphic fields include:
-
Owner: This field represents the parent of the record. For example:This example query works for Task records whose owners are either Calendars or Users.
-
Who: This field represents the person associated with the record. For example:This example query works for Task records whose owners can be either Calendars or Users, and whose
Whofield can be either Contacts or Leads.If you’d like to know the type of object returned in a query, use
Who.Type. For example:Using this example, you can query all the Tasks associated with Contacts:
-
What: This field represents nonhuman objects that are associated with the record. For example:This example query works for Events where
Whatcan be an Account or a Solution, or any of another number of object types.
You can use the Type qualifier on a field to determine the object type that’s referenced in a polymorphic relationship. Use the Type qualifier in the WHERE clause of a SELECT statement to conditionally control what’s returned from the query depending on the referenced object type. The following SELECT statement uses Type to filter the query based on the What field in Event.
At run time, this SELECT statement returns the IDs for Events that reference Accounts or Opportunities in the What field. If an Event referenced a Campaign in the What field, it wouldn’t be returned as part of this SELECT. Unlike the TYPEOF expression, object types are returned as strings from Type. You can apply any WHERE comparison operator to the object type strings, such as = (Equals) or LIKE.
SOQL supports polymorphic relationships using the TYPEOF expression in a SELECT statement. 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.)
Use TYPEOF in a SELECT statement to control which fields to query for each object type in a polymorphic relationship. The following SELECT statement returns a different set of fields depending on the object type associated with the What polymorphic relationship field in an Event.
At run time, this SELECT statement checks the object type referenced by the What field in an Event. If the object type is Account, the referenced Account’s Phone and NumberOfEmployee fields are returned. If the object type is Opportunity, the referenced Opportunity’s Amount and CloseDate fields are returned. If the object type is any other type, the Name and Email fields are returned. Note that if an ELSE clause isn’t provided and the object type isn’t Account or Opportunity, then null is returned for that Event.
Note the following considerations for TYPEOF.
-
TYPEOFcan’t be used with a relationship field whosenamePointingattribute is false. -
TYPEOFcan’t be used with a relationship field whoserelationshipNameattribute is false. -
TYPEOFis only allowed in theSELECTclause of a query. You can filter on the object type of a polymorphic relationship using theTypequalifier in aWHEREclause. For details, see Filter on Polymorphic Relationship Fields. -
TYPEOFisn’t allowed in queries that don’t return objects, such asCOUNT(). -
TYPEOFcan’t be used in SOQL queries that are the basis of Streaming API PushTopics. -
TYPEOFcan’t be used in SOQL used in Bulk API. -
TYPEOFexpressions can’t be nested. For example, you can’t useTYPEOFinside theWHENclause of anotherTYPEOFexpression. -
TYPEOFisn’t allowed in theSELECTclause of a semi-join query. You can useTYPEOFin theSELECTclause of an outer query that contains semi-join queries. The following example is not valid becauseTYPEOFis used in the semi-join query:The following example is valid because
TYPEOFis only used in the outerSELECTclause: -
TYPEOFcan’t be used in queries with functions in theSELECTclause. The following example is not valid because theTYPEOFincludes theFORMATfunction.Instead, run the same query without functions to retrieve a list of IDs.
Then, run a second query with functions on the resulting ID list.
-
TYPEOFcan’t be used in queries withGROUP BY,GROUP BY ROLLUP,GROUP BY CUBE, andHAVING.
You can combine TYPEOF and Type in a SELECT statement. The following SELECT statement uses both TYPEOF and Type to filter the query and refine the set of returned fields based on the What field in Event.
At run time, this SELECT statement always returns the ID for an Event, and then either Account.Phone, or Opportunity.Amount, depending on the object type referenced by the Event’s What field. Note that no ELSE clause has been provided. Since this statement filters based on the What field in the WHERE clause, only Events that reference either an Account or Opportunity are returned, so the ELSE clause is not needed. If an ELSE clause was included in this case, it would be ignored at run time.
In the Enterprise and Tooling API WSDLs, the object type of a polymorphic field depends on the version of the API.
-
In API version 46.0 and later (and in versions where the Developer Preview part of the SOQL Polymorphism feature is enabled), the object type is
sObject. For example: -
In other versions, the type is
Name. For example:
This affects Java code generated from these WSDLs. For example, in Task.java, the Owner field is now defined as:
For more information about generating Java code from WSDL files, see Setting Up Your Java Developer Environment.
See Also