Find your content:

Search form

You are here

Can I create a polymorphic key from a custom object?


I'm creating a custom object that needs to be related to multiple other objects (but only one per record). 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.

I can't do this via the UI as the lookup can only be related to a single type.

New Lookup Relationship

Is it possible to create a polymorphic reference through another means, such as the metadata api?

Failing that, are there any viable alternatives?

I'd like to use the lookup so I can show the related objects via the page layouts. This object will also be deployed as part of a managed package and clients may want to reference their own custom objects from it.

Attribution to: Daniel Ballinger

Possible Suggestion/Solution #1

As of now, polymorphic keys only exist as standard fields. See the idea - Custom Polymorphic Id field (WhoId/WhatId)

To mimic the functionality by say using a custom text field, would be a rigorous customization:

  1. Custom text field, mark as ExternalId for indexing purpose.
  2. UI control...probably would have to be custom visualforce + apex controller.
  3. Related Lists...again, custom visualforce + apex controller.
  4. Probably some other stuff I can't think of right now, but that would be the minimum as I envisage it.

Attribution to: pchittum
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content