I have a string which is holding email ids of users from my org. Now I have few custom metadata type records which are holding email IDs of users who are inactive.

Now I need to compare the string which has all my users' email IDs with that of custom metadata type Email field.

It is like

If(allEmailIds == Inactive_Email__c){ //allEmailIds is the string holding my org users' email ids
//do the logic here;

I knew the query to fetch custom metadatatype records into apex but I'm not getting the logic to compare these two values. Below is the query of my custom metadata type object

Select MasterLabel, Inactive_Email__c from InactiveUsers__mdt

Can anyone please help me out with this requirement.

  • Why do you have this custom metadata? What problem are you trying to solve?
    – sfdcfox
    May 29, 2020 at 7:31
  • The above given scenario is sort of example this which is similar to my business requirement. The requirement is to compare two strings in IF condition which are holding some field values and then apply the logic. Please suggest
    – Sastri
    May 29, 2020 at 7:43
  • Why do you need to store inactive user list in custom metadata type? User object has the IsActive field. May 29, 2020 at 7:55

2 Answers 2


As comments suggests there might be some better solutions to get or store InActive Users information rather than using Custom Metadata Type records for it.

But to your question - Sounds like you are trying to get a field value (Inactive_Email__c) from a Custom Metadata Type records stored on InactiveUsers__mdt and then compare it with a different string value received as Input or from a different Query.

Below Example can show a way to achieve this...

InactiveUsers__mdt[] InActiveEmailRecords = [Select MasterLabel, 
Inactive_Email__c from InactiveUsers__mdt];
  Set<String> InActiveEmails = new Set<String>();
  for(InactiveUsers__mdt record:InActiveEmailRecords) {
  //Example Query for User based on the InActive Email addresses in mdt
  User[] InActiveUsers = [Select Id,Name,Email From User WHERE IsActive=false AND Email IN:InActiveEmails ];

 // OR If you want to match with a certain string
 for(String email:InActiveEmails) {
      if(email == "YourString"){
          // Your logic goes here...
  • The second option shows comparison of each email stored on mdt to a string. if the string holds multiple emails, you probably will need to split the string first and then iterate over it to compare.
    – sfdx bomb
    May 29, 2020 at 9:44

From Spring 21 there is new way for accessing record inside Custom Metadata.

Accessing specific record:


Accessing all records (and you can loop them):


In your case, you can use getAll() method and loop into them and compare them.


Map<String, ObjectName__mdt> mapCMD = ObjectName__mdt.getAll();
for(String keySetString : mapCMD.keySet()){

Note: getInstance method is a new addition that is supported on API version v51 and upwards. You will need to update your Class/Trigger version to at least v51.

You must log in to answer this question.

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