Contents
How to use polymorphic fields in soql queries?
If you want more general information on polymorphic relationships, see Understanding Relationship Fields and Polymorphic Fields in the SOQL and SOSL Reference. You can use SOQL queries that reference polymorphic fields in Apex to get results that depend on the object type referenced by the polymorphic field.
Can you query a polymorphic field with security enforced?
There are some restrictions while querying polymorphic lookup fields using WITH SECURITY_ENFORCED. Polymorphic fields are relationship fields that can point to more than one entity. Traversing a polymorphic field’s relationship is not supported in queries using WITH SECURITY_ENFORCED.
When to use typeof in a soql query?
TYPEOF is used in a SELECT query to specify the fields to be returned for a given type of a polymorphic relationship. For example, you cannot use WITH SECURITY_ENFORCED in this query. The query specifies certain fields to be returned for Account and Opportunity objects, and Name and Email fields to be returned for all other objects.
If field access for either LastName or Description is hidden, this query throws an exception indicating insufficient permissions. If field access for Website is hidden, this query throws an exception indicating insufficient permissions.
Which is a relationship field and which is polymorphic field?
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.
When to use typeof in a polymorphic query?
TYPEOF is only allowed in the SELECT clause of a query. You can filter on the object type of a polymorphic relationship using the Type qualifier in a WHERE clause, see Filtering on Polymorphic Relationship Fields for more details.
Can a polymorphic key be attached to a custom object?
I.e. via a ParentId field it could be related to a Opportunity, a Contact, an Account, or another custom object. This would be pretty much the same as the Polymorphic Key Attachment.ParentId that can reference multiple other object types.