function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mads Peter RommedahlMads Peter Rommedahl 

How to have a Custom Metadata list and use it in Apex code?

Hey, I currently have an Apex function which takes a name and an existing Contact, and if the current Contact name is our dummy name 'Dyreven' but the new name is NOT it updates the Contact name. Like this:
 
// Small method to check if the passed name variable is NOT 'Dyreven' but the current name is. Then replace 'Dyreven' with the new name

    private static Contact checkForUpdatedName(String name, Contact con){
        if (name != 'Dyreven') {
            if (con.LastName == 'Dyreven') {
                con.LastName = name;
                update con;
            }
        }
        return con;
    }

 
However, instead of having the dummy name hardcoded in the Apex I want my Apex to pull a list of dummy names from custom metadata and then check through all of them. So it should be possible to have a custom list of dummy names and then the Apex code would check against that list instead of against a hardcoded String like above.

How can I do this? Are lists supported as a custom metadata type?
Best Answer chosen by Mads Peter Rommedahl
Om PrakashOm Prakash
Hi,
I think you are looking something similar to my bellow code sample.
I retrieved names from meta data Name_Data__mdt and added in a Set according to your use case.
private static Contact checkForUpdatedName(String name, Contact con){
	Set<String> setAllName = new Set<String>();
	for(Name_Data__mdt obj : [SELECT Contact_Name__c FROM Name_Data__mdt]){
		setAllName.add(obj.Contact_Name__c);
	}
	 
	if (!setAllName.contains(name)) {
		if (setAllName.contains(con.LastName)) {
			con.LastName = name;
			update con;
		}
	}
	return con;
}

 

All Answers

Prashant Pandey07Prashant Pandey07
Hi Mads,

If you have only one string to compare then you can try below code.
 
private static Contact checkForUpdatedName(String name, Contact con){
        Namemetdata_Type__mdt n=[SELECT name__c,DeveloperName FROM Namemetdata_Type__mdt limit 1];
        if (name != n.Name__c) {
            if(con.LastName == n.Name__c) { //Name__c will hold your string
                con.LastName = name;
                update con;
            }
        }
        return con;
    }

 
Om PrakashOm Prakash
Hi,
I think you are looking something similar to my bellow code sample.
I retrieved names from meta data Name_Data__mdt and added in a Set according to your use case.
private static Contact checkForUpdatedName(String name, Contact con){
	Set<String> setAllName = new Set<String>();
	for(Name_Data__mdt obj : [SELECT Contact_Name__c FROM Name_Data__mdt]){
		setAllName.add(obj.Contact_Name__c);
	}
	 
	if (!setAllName.contains(name)) {
		if (setAllName.contains(con.LastName)) {
			con.LastName = name;
			update con;
		}
	}
	return con;
}

 
This was selected as the best answer
Mads Peter RommedahlMads Peter Rommedahl
Both good answers :)
However, for my need I ended up simply using a CustomLabel, splitting the String along , and then I had a simple list of names to use
Om PrakashOm Prakash
Sounds good for 1,000 characters in length.
Yes our answer was based on custom metadata as you referred the same in question itself.
Enjoy Coding :)