Can we have a custom object similar to Attachment object where the parent can be any of the "Account", "Contact", "Lead" etc?


The above links says

"The following objects are supported as parents of attachments:

  • Account
  • Asset
  • Campaign
  • Case
  • Contact
  • Contract
  • Custom objects
  • EmailMessage
  • EmailTemplate
  • Event
  • Lead
  • ...."

Basically, I want a custom object that can be a child of any of the above standard object.


No, you can't. SFDC does not allow the creation of polymorphic lookups (lookups like Attachment.Parent that can point to one of several objects).

What you can do is create a lookup to each of the objects you want to have as a parent, and populate only one of those per child object. So any given child links to one of the parents, using its parent specific field.

I would likely want a VF or several to view/edit the Child Object so it didn't show lots of empty/irrelevant fields.

| improve this answer | |
  • 1
    And there is a limit to the number of lookup fields an object can have... – sfdcfox Oct 10 '14 at 15:51

If you are willing to build all the UI yourself, you can approximate this by defining an 18 character text field (lets call it ParentId__c) on your custom object and populating that with the ID of whichever type of object you like. (You should also get an index added to that field.)

I have not tried this (so test first), but would assume that queries like:

select Name from Contact
where Id in (select ParentId__c from MyCustomObject__c where Id = ...)

would work, though any queries using __r relationships wouldn't.

UI features like related lists and lookup pops wouldn't work - lots of Visualforce would be needed.

| improve this answer | |
  • I've used this construct before and it works fine as long as you stick with Apex/VF, because as you say __r relationships don't work with it, which scuppers cross object formulae, standard reporting and related lists. – Dominic Oct 10 '14 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.