[SalesForce] Custom Metadata vs. Custom Objects in Managed Package

I worked on Managed packages for nearly 10 years now and never missed Custom Metadata Types. Not before they came and not after they were announced. I didn't come across a single great and technical severe reason to use Custom Metadata Types over regular Custom Object.

Unlike Custom Objects they:

  1. Don't support triggers
  2. Are hard to create in Apex Tests
  3. Have a very limited number of field types
  4. Have an inflexible UI to managed records

The only use case where CMDT are somewhat nice is, that you cannot package Custom Object records in packages. But that is also not an issue. You can perfectly store hundreds of Custom records in JSON serialized form in a single packaged Static Resource and unpackage them on install.

So tell me what your reasons are to favor them over Custom Objects?

Best Answer

Here are some "advantages" vs using a Custom Object:

  • Can be Deployed
  • Can use Field Definition or Entity Particle to create references to Custom Object Metadata
  • Retrieve doesn't cost against SOQL limits (unless it contains long text)
  • Can be referenced in formula fields
  • Unit Testing isn't too bad as well as the code is well composed. Because they are treated similar to @seeAllData, it actually makes integration testing the current configuration of the ORG much easier.
  • Can be "protected" to store secrets in managed packages

However (IMO), Salesforce missed too many features to make them the clear choice in every scenario:

  • Records cannot be statically referenced in code (this would make dependencies much more robust)
  • No direct/global access in VF or LWC
  • Objects cannot reference them. This results in a lot of "magic" relationships being setup (EG string matching).
  • No Triggers (As mentioned above)

I've created a public google sheet that attempts to compare the features of each of the configuration options (please add comments if I missed anything).

My Approach

  • I tend to favor CMDT over Custom Settings, UNLESS I need user specific configuration.
  • I'll use Custom Objects where it's critical to ensure referential integrity to data records.
  • Custom Labels were once commonly used because they could be deployed, but now with CMDT there isn't much reason to use them (for configuration).
Related Topic