I am new to custom metadata types (CMDT). With the Winter'22 maintenance, I learned that there is no longer a need to write Salesforce Object Query Language (SOQL) to access custom metadata records in Apex. There are new methods similar to accessing Custom Settings. This removes any SOQL limits, making the code cleaner and faster.

However, the example shown for this on the trailhead module uses

Games__mdt mc = Games__mdt.getInstance('FirstRecord');

to return a single record sObject. FirstRecord is the developerName for the CMDT named Games_mdt

I am interested in learning good practices with Apex and I am curious to know:

  • Is hard coding CMDT names in Apex a good practice?
    • If so, please explain why?
    • If not, how do I avoid hard coding CMDT name?
  • 6
    Naven, I posted an answer on the previous version of this question - can you clarify what you are still confused about? I'm not sure where the idea comes from that it is bad to use type names.
    – David Reed
    Commented Feb 15, 2022 at 4:35
  • I am not familiar with such usage in Java so I am a little confused why in Apex would this be the case. I am genuinely curious but I am getting downvoted for asking questions and I don’t know why.
    – Naven
    Commented Feb 15, 2022 at 4:44
  • I did not downvote your question myself, but you might make an edit to clarify what your concern is. Apex isn't Java, although there are many similarities; referring to types explicitly in Apex is 100% normal.
    – David Reed
    Commented Feb 15, 2022 at 4:52
  • I’m afraid that might receive even worse reaction because this forum is for Salesforce users. I wish I could find material to read and help myself instead of asking stupid questions on this forum. Is the specific coding vs generic structures something I’ll learn on the job instead of teaching myself?
    – Naven
    Commented Feb 15, 2022 at 5:02
  • I doubt downvote is given because you asked something different, you can find many questions on this platform on same topic "hardcoding in apex". Probably you could have edited your question to be precise and mention that difference between other existing questions. As far as my understanding, you can hard code something which doesn't change on deploying it to other environments, whereas if you hardcode a recordId and once it is deployed it changes where as the name won't and especially if you have a developer name it is much better cause it would be unique too. Commented Feb 15, 2022 at 7:24

1 Answer 1


Generic or specific coding structures are tools. Knowing when to use which is part of the art involved in being a good developer.

In this case, the name of your Custom Metadata Type is Games__mdt, and the only alternative would be to use Dynamic SOQL which would be an awkward fit for this situation. In all likelihood, trying to be more generic in this dimension would make your code harder to read and therefore less maintainable. It may also have worse performance as far as CPU consumption.

  • Thanks. Sorry for my poor language on the question but my question precisely is how do I learn that art of knowing when to use generic vs specific coding structure?
    – Naven
    Commented Feb 15, 2022 at 4:53

You must log in to answer this question.

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