function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AV-Force.CoderAV-Force.Coder 

How to use Polymorphic keys

Hi,
 
Can somebody please explain how to use Polymorphic keys. I'm just not getting any information on it.
 
Thanks and Regards,
A.V.
mikefmikef
Hi

Please start by reading this doc.

One thing that might help; think of polymorphic keys in apex as an SObject that holds child or parent SObjects.

So one account query can bring back all the contacts as well.
Then you can reference the contacts through the account SObject that you created from the query.

AnshulVermaAnshulVerma

Hi Mikef,

I've already read the article that you mentioned.But, what i am not able to figure out is, how to activate the polymorphic keys functionality. My organization account has been updated to Summer 08' release but still i do not see any option in "Choose the field type"  setp (while creating a new Custom Field). I am not able to see and reference/polymorphic data type.

Please provide the necessary information regarding this issue.

Thanks in advance,

A.V.

AnshulVermaAnshulVerma
Infact, i think that the actual question lies in "how to create a Reference field" ??
mikefmikef
So an referrance field is a lookup to another object.

Example: the AccountId field on a Contact record.

You can create a lookup when you create a custom field it's one of the data type choices.
JasonRogersJasonRogers
Mike,

What you describe ("one account query can bring back all the contacts as well") is not polymorphic keys -- it's just a reference. The doc you reference has some information on polymorphic keys, but only to the extent that they exist.

A polymorphic key is a field that can point to multiple sobject types (eg. WhoId on a Task is either a Lead or a Contact).

The original poster was asking how to setup such a relationship in an object.  As far as I know, SFDC only allows non-polymorphic relationships to be defined by the user.  So, you can create a relationship between a custom object and an Account, but you cannot have one field that is able to point to either an Account, a Contact, or an Opportunity.

--
Jason
mikefmikef
thank you for explaining this, I was a little confused as well.
AnshulVermaAnshulVerma
I apologize if there was confusion due to my remarks, but my case is as follows:-
 
I have to register various alerts on modifications of a Case, Comapany etc., so i have a custom object named "ActionAlert". So, each time i do something on Case, Company or other i would save a new recordin ActionAlerts and would be displayed to the concerned users. Now, the ActionAlert has a field RecordID (pointing to record to which it pertains like Case, Company etc.).

So, this RecordID field is a reference field which references to multiple objects.

Please explain how to achieve this??

---> JasonRogers is it right that we(developers) cannot create a polymprphic key??

AV

JasonRogersJasonRogers
I believe it is true that we, as developers, cannot create polymorphic references.  However, there is nothing stopping you from creating a text (of length 18) that "represents" the polymorphic relationship.  The disadvantage is that you will not be able to use built-in related lists to display these on related objects, but with VisualForce pages that is mostly a moot point.