Filter Through Parent Relationships
In the earlier sections of Filter Results, we’ve discussed how fields are mapped into the filter type and how to combine filters with the
NOT functions. In addition, each parent relationship the sObject is added to the filter type as well.
A polymorphic relationship is one where an object can be one of several object types. For example, an event can have a a polymorphic relationship to an account, campaign, or opportunity via the Related To field. Contrastingly, a contact has a non-polymorphic relationship to an account via the Account Name field.
If the parent relationship is non-polymorphic, then the corresponding filter type for that object is used.
Consider an account that as a parent relationship
CreatedBy that points to the User object.
As the relationship is non-polymorphic, the filter has a field with the same name of the parent relationship with the corresponding filter type.
To find all accounts created by a particular user, we can construct a
where argument as follows.
This query is translated to the following SOQL statement.
If the parent relationship is polymorphic, then an input object following the naming convention
<ObjectName>_<RelationshipName>_Filters is constructed and used as the filter type for that parent relationship. For example, the
Who filter has a type of
SocialPost_Who_Filters. This input object has one input object field for each concrete type that participates in the polymorphic relationship.
The union has one possible type for each concrete type that participates in the polymorphic relationship.
The input object type has one input object field of the filter type for each object that participates in the polymorphic relationship.
With this structure, it's possible to filter the result set by the value of the underlying polymorphic types data. Each filter is combined with an
AND condition to assert the polymorphic relationship is of the filter type.
The query is translated to the following SOQL statement.
You can work with multiple fields on a polymorphic filter.
When translated to SOQL, the individual clauses are joined with
OR. which allows selective filtering based on the underlying type of the polymorphic relationship.
Although objects with an
WhoId field have polymorphic parent relationships, GraphQL API includes as possible types only those objects that are available via the User Interface API.
When querying for records using a polymorphic parent relationship, you can order and filter by the Name object. Using the Name object enables you to filter and order using the fields supported on the Name object. If you ask for a filter on the
relationshipName.FirstName field and the underlying object is a Contact, then the filter is applied to that object. Otherwise, if the object is of a different type that does not have the FirstName field, the filter evaluates to false and no record is returned.
Only the Name object is currently supported for ordering and filtering in a polymorphic parent relationship. Other object types can result in a runtime error and might not be available in a future release.
For example, the
Who relationship field in a SocialPost object can reference an Account, Contact, or Lead. Let's say you want to find the underlying record with a specific email field value using the
Who relationship field.
Similar to an
ORDER BY or
WHERE clause, the above query translates to the following SOQL.