2

In Salesforce there are some specific objects like Attachment, RecordType etc. which are 'closed', i.e. we cannot create custom fields on them, in opposite to 'open' objects, e.g. Account, Contact etc.

So the question here is - how to detect from Apex whether we are dealing with 'open' or 'closed' object relying just on provided object name? Really want to avoid hard-coding list of object names)

2 Answers 2

2

https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/tooling_api_objects_entitydefinition.htm

If you look it up via EntityDefinition the field IsCustomizable is described as If true, custom fields can be defined for the entity. And this field is available even by SOQL so no API call needed.

For example:

SELECT QualifiedApiName, IsCustomizable 
FROM EntityDefinition 
WHERE QualifiedApiName IN ('Account','Contact','RecordType','Attachment')
2
  • That's awesome. Thanks a lot, man. The only downside is that it eats query and record limits, but I probably just want too much at once)
    – wesaw
    May 1, 2018 at 11:49
  • This is true. Unfortunately SFDC kind of haphazardly splits the Apex-accessible object metadata between EntityDefinition and Describe. Are you building this for a single org or a managed package? For single-org projects if I want to cut down on repetitive queries I sometimes use Platform Cache.
    – Charles T
    May 1, 2018 at 12:33
0

I think your only real solution is to call describeSObject or SObject Describe, and check if layoutable is true. I'm pretty sure that objects we can customize have layouts we can describe, and objects we can't do not, but unfortunately, we can't get at this data directly from Apex Code, hence the need for an API call. Hopefully, we'll have a way to determine this in the future, but for now, it's probably safer to build a white list of known-safe items that can be customized, or be willing to call the Metadata API with a Check Only deployment to see which objects return errors, which would definitely not be a very user-friendly wait time.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .