Faking Polymorphic Lookups with Flow Invocable Method
One of the most requested features for the salesforce platform is the ability to create a polymorphic lookup custom field. If you are unfamiliar with what a polymorphic lookup is it’s a single lookup field that can be related to any salesforce object. Right now the light bulb might be going off about how useful this functionality would be or you are extremely confused. For those that are not seeing the value in this capability think about the task object and the WhatId and WhoId fields - these are polymorphic lookups. Think about how elegant it is to be able to relate a task to any object without having multiple lookups. Think about how this enables simplicity in reporting and solution design.
It’s safe to assume that this capability will not be being brought to light by Salesforce - the idea has been sitting on the ideaexchange since 2009 with close to 20,000 points with a current status of Not Planned. Rather than be blocked by Salesforce we have developed a methodology here at Campfire to fake polymorphism. To implement a polymorphic lookup on any object we create the following fields.
Fields to Create
Field Label | Field API Name | Field Type | Description |
---|---|---|---|
Related To Id | RelatedToId__c | Text (18) | Id of the related record. |
Related To Object | RelatedToObject__c | Text (100) | Object Label of the Related Id |
Related To Object API Name | RelatedToObjectApiName__c | Text (100) | Object API Name of the Related Id |
Related To Record Name | RelatedToRecordName__c | Text (100) | Record Name of the Related Id |
Related To | RelatedTo_f__c | Formula (Text) | A hyperlink formula field to link to the related record that is displayed to end users. We write this formula to display [Related To Object]: [Related To Record Name]. IF( NOT(ISBLANK( RelatedToId__c )), HYPERLINK("/"+ RelatedToId__c ,RelatedToObject__c &": "& RelatedToRecordName__c ,'_self'), NULL ) |
Next, let’s explore how we can populate these fields with flow. For the sake of reusability, we will build a single auto-launch flow that we will use for getting all of the details about a record Id. How we would dynamically get the object label and API name without writing code would be to first create a formula field that returns the first 3 characters of the Id (i.e. the entity key prefix).
Next, we will query (i.e. Get Record) the Entity Definition Object (review the object schema). We will return records where the KeyPrefix is equal to are RelatedToId prefix formula we created.
As we are always trying to think like a developer and reduce system load we will only return the fields from the Entity Definition object that we care about. The QualifiedApiName and the MasterLabel (review how to think like a developer when building flows).
Now, assuming we passed through the record name into the flow, we have assembled all the elements that we need to populate the custom fields to support the polymorphic lookup on our object. It’s that simple.
Flow Invocable Action to Get Record Id Details
But it can always be simpler. Here at Campfire we have taken this idea of a polymorphic lookup even further and made it simpler to implement. We have developed an invocable method that can be used within flow where you provide the action the record Id and it returns for you the record name, object name, and object API name. If interested test the action out now!
WARNINGS
There are several limitations to be cognizant of with this approach of faking a polymorphic lookup. First, you will not have a related list out of the box. Fortunately, you can easily create a screen flow with the data table element to serve as that related list that would work on any object as we have just a single field to match against (i.e. the Related To Id). Secondly, be aware of salesforce reporting limitations (if you report in a tool such as Tableau or another advanced BI tool ignore this limitation as you can easily join data tables together). As we do not have a direct link to the record we can not build a report that would pull in details from the related record.
Now that you have been enabled to implement polymorphic lookups what will you build first? Let us know how you leverage this methodology in your solutions in the comments!