WHAT ARE POLYMORPHIC FIELDS IN SALESFORCE?

Shrey Soni
4 min readFeb 14, 2021

Hello everyone,

Let us know about “POLYMORPHIC FIELDS”.

First lets google what does polymorphic means and here is what I found,

By this meaning, given by google we can roughly translate our topic for today as : A KIND OF FIELD WHICH POINTS TO ONE OR MORE OBJECT.

Confused?

Let us simplify this concept in terms of programming:

PROGRAMMING: You have 3 instances of two different object

A(a) : Instance A of class ‘a’.

A1(a) : Instance A1 of class ‘a’.

B(b) : Instance B of class ‘b’.

If you try to assign A1 to A it will easily get assigned as they are instances of same object.

Where as if you try to assign A with B, Our compiler will throw an error saying “Cannot assign Object A(a ) with value B(b)”.

But here if you have something like a generic instance (a general variable which accepts all kind of data) feature in Instance A which can store instance of itself and also of other objects.

This would save us a lot of overheads that come along with multiple fields to store different data for different objects.

HOW IS THIS USEFUL ?

Polymorphic fields gives the salesforce a upper hand over using single field for multiple usage by tracking a dedicated metadata related to that field in order to control its polymorphic nature, allowing the ecosystem to have a field that can act according to the usage.

WHAT KIND OF FIELDS ARE WE TAKING ABOUT ANY EXAMPLES ?

Sure I was coming to it, fields such as :

  1. OwnerId field on the records of all the objects we see.
  2. Who field on objects such as Task, Event etc.

WANNA KNOW MORE ?

But polymorphism of these fields are controlled by the usage of its metadata / properties.

HOW IS IT CONTROLLED BY METADATA / PROPERTIES ?

Relationship fields contains three properties which holds the nature how this field will act in a specific environment.

Lets take an example of OwnerId field:

The OwnerId field of the Account object has the following properties:

relationshipName = Owner

namePointing = false

referenceTo = User

This tells that this is a relationship field and not a POLYMORPHIC.

“relationshipName” acts as a pseudo field to get information about the related object, whose type is specified in the “referenceTo” field.

But now pay a close look over the properties values,

The OwnerId field of the Event object has the following properties:

relationshipName = Owner

namePointing = true

referenceTo = Calendar, User

This means it is a polymorphic field.

Owner could be a Calendar, or a User.

DID YOU SEE IT ?

Yes, the “namePointing” property’s state decided the state of the field (Relationship/Polymorphic)

Also “referenceTo” field gave essence to the polymorphic nature of the field (Type of object’s values it can hold for this instance it is Calendar and User).

Now I guess it is a bit more clear what and how is polymorphism achieved.

HOW TO KNOW IF A FIELD IS A RELATIONSHIP OR A POLYMORPHISM FIELD ?

To determine what kind a field is, call “describeSObjects()” on the object and examine the properties for the field.

  1. If relationshipName is not null, the field is a relationship field.
  2. If, in addition, namePointing is true, then the field is polymorphic.

USE THIS:

Schema.DescribeSObjectResult[] descResult = Schema.describeSObjects( new String[]{‘Account’, ‘Event’});
system.debug(‘DETAILS + ‘ + descResult);

Lets get down on the usage,

I am putting down bunch of examples which will make things much more clearer.

  1. SELECT Id, Owner.Name FROM Event WHERE Owner.Type = ‘User’

The above SOQL selects all the event records with the Owner type as User.

So this shows as per the previous explanation “referenceTo” property of a relationship field can be accessed by using “Type” keyword.

2. SELECT Id, Who.Id, Who.Type FROM Task

This SOQL uses the who relationship field of task object which can be capture contact and account both.

3. SELECT Id, Who.FirstName, Who.LastName
FROM Task
WHERE Owner.FirstName LIKE ‘SHR%’

FUN FACT:

POLYMORPHIC FIELDS have limitations when used along with the “WITH SECURITY_ENFORCED” keyword in SOQL read the following article for more information.

Hope this clears out most of the basic questions related to polymorphic fields.

I will be updating more in this blog as I learn more about polymorphic fields and where all it is used and its related properties for different use cases.

Feel free to let me know if I have misunderstood anything about polymorphic fields in the comments or join me on my discord channel : https://discord.gg/ZhgwaWxAKr.

Would love to read the reviews.

CHEERS !!!

--

--

Shrey Soni

Software professional who loves to write and share. Have lots of stories and learnings related to life, food, salesforce, blockchain & photography keep reading