I would like to do a generic function to get custom meta data type records and return it as a Map. In a first time with just one field , that means Map<String,String>
and in a second time with multiple fields Map<String,Map<String,String>>
.
The key will be the DeveloperName. In case there is just one field map<String,String>
the second string will be this field value.
In case there are multiple fields Map<String,Map<String,String>>
in the nested Map the key will be the API Name of the field and the second string the value of the field.
This function work when the name of the custom meta data type is not in a variable but here it's not working and I don't know how to fix it
public static Map<String, String> getCustomMDTToMap(String cmd_Api_Name, String cmdt_Field){
System.debug('++++++++++++getCustomMDTMap+++++++++++++');
List<SObject> customMDTList = new List<SObject>();
String query = 'SELECT DeveloperName ,'+cmdt_Field+' FROM '+cmd_Api_Name;
try{
customMDTList = Database.query(query);
}catch(QueryException ex){
System.debug(ex);
}
System.debug(customMDTList);
Map<String, String> customMDTMap = new Map<String, String>();
for(SObject cMDTItem : customMDTList)
{
customMDTMap.put(cMDTItem.DeveloperName,cMDTItem.cmdt_Field);
}
System.debug(customMDTMap);
System.debug('------------getCustomMDTMap-------------');
return customMDTMap;
}
I would like to know first if this is a good way to use custom meta data type, if no why ? And if you have any idea how to fix the function .