Event and Task (Beta) available in API v59.0 and later
A polymorphic relationship is one where an object can be one of several object types. For example, an event can have a polymorphic relationship to an account, campaign, or opportunity via the Related To field. Conversely, a contact has a non-polymorphic relationship to an account via the Account Name field.
To query using a polymorphic relationship, use inline fragments with the ... on Object syntax.
Owner Polymorphic Field
The Owner field represents the parent of the record. For example, a case owner can be a Group or User object.
When the parent relationship is polymorphic, an input object following the naming convention <ObjectName>_<RelationshipName>_Filters is constructed and used as the filter type for that parent relationship.
The Owner field follows the naming convention <ObjectName>_Owner, which is used as the filter type for that parent relationship.
For example, the Case object’s Owner filter has a type of Case_Owner_Filters.
Case Owner field
1type Case{2 Owner: Case_Owner3}
The union has one possible type for each concrete type that participates in the polymorphic relationship.
Case_Owner subtypes
1union Case_Owner = Group | User
The input object type has one input object field of the filter type for each object that participates in the polymorphic relationship.
Let’s say you query cases that match a specified Owner.Name value. Case records correspond to owners that are either groups or users. This example queries cases where the owner’s username starts with “D” and the Owner.Type is a User object.
The Who field represents the person associated with the record. Some objects, like Event, Task, ContactRequest, and SocialPost, have a Who field. For more information, see the Object Reference for the Salesforce Platform.
For example, the ContactRequest object’s Who filter has a type of ContactRequest_Who_Filters. This input object has one input object field for each concrete type that participates in the polymorphic relationship.
ContactRequest with who field
1type ContactRequest{2 who: ContactRequest_Who3}
The union has one possible type for each concrete type that participates in the polymorphic relationship.
ContactRequest_Who union
1union ContactRequest_Who = Contact | Lead | User
ContactRequest_Filter input
1input ContactRequest_Filter{2 who: ContactRequest_Who_Filters # the filter object contains another filter object constructed for the polymorphic type3}
The input object type has one input object field of the filter type for each object that participates in the polymorphic relationship.
ContactRequest records have a Who field that can reference a contact, lead, name, or user. This example query applies to ContactRequest records whose owners can be either a Group or User object. It filters the records based on the User object and select records where the Who.Type is a Contact object.
The previous query is similar to this SOQL statement.
SOQL statement with polymorphic Who and Owner fields
1SELECT Id, Who.FirstName, Who.LastName2FROM ContactRequest3WHERE Owner.FirstName LIKE 'D%'
What Polymorphic Field
The What field represents non-person objects that are associated with the record. Some objects, like Event and ContactRequest, have a What field. For more information, see the Object Reference for the Salesforce Platform.
For example, the ContactRequest object’s What filter has a type of ContactRequest_What_Filters. This input object has one input object field for each concrete type that participates in the polymorphic relationship.
ContactRequest type
1type ContactRequest{2 who: ContactRequest_What3}
The union has one possible type for each concrete type that participates in the polymorphic relationship.
ContactRequest_What union
1union ContactRequest_What = Account | Case | Opportunity
ContactRequest_Filter input
1input ContactRequest_Filter{2 What: ContactRequest_What_Filters # the filter object contains another filter object constructed for the polymorphic type3}
The input object type has one input object field of the filter type for each object that participates in the polymorphic relationship.
ContactRequest records have a What field that can reference an account, case, name, or opportunity. This example query applies to ContactRequest records whose What field is Account. It returns ContactRequest records with the Name field, the What.Name field where the What.Type is Account.
The previous query is similar to this SOQL statement.
SOQL statement with polymorphic What field
1SELECT Id, Name, What.Name2FROM ContactRequest
Filter Polymorphic Fields with Boolean Operators
You can filter the result set by the value of the underlying polymorphic types data. Each filter is combined with an and operator to assert the polymorphic relationship is of the filter type.
1SELECT Id, Name2FROM ContactRequest3WHERE Who.FirstName LIKE 'R%' AND Who.LastName LIKE 'G%'
You can work with multiple fields on a polymorphic filter with an or operator. This example returns ContactRequest records with the Id, Name, and What.Name fields where the What.Type is Account. It filters the records where the What.Name is “Salesforce” or the Who.FirstName contact names start with “R”.
The previous query is similar to this SOQL statement where the individual clauses are joined with OR, allowing selective filtering based on the underlying type of the polymorphic relationship.
SOQL statement with selective filtering
1SELECT Id, Name, What.Name2FROM ContactRequest3WHERE What.Name = 'Salesforce' OR Who.FirstName LIKE 'R%'