1

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 .

1

In your code, you're trying to assign a Metadata Type to a List<SObject>, but Metadata Types are not SObjects, they are their own distinct type (i.e. custom sobjects end in __c while Custom MDT end in __mdt)

Since you don't know the MDT at runtime, try using List<Object> and use the .get() to access the actual values. Something like:

List<Object> mdts = Database.query('SELECT ... ');

for (Object mdt : mdts) {
    customMDTMap.put(mdt.get('DeveloperName'), mdt.get(cmdt_Field));
}
  • thanks , I get this error : Method does not exist or incorrect signature: void get(String) from the type Object – Raphael D Jul 11 at 7:45
0

Ok I found out how to do this ,please let me know if there is a better way to use custom meta data type.

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);
    }
    Map<String, String> customMDTMap = new Map<String, String>();
    for(sObject cMDTItem : customMDTList)
    {
        customMDTMap.put((String)cMDTItem.get('DeveloperName'),(String)cMDTItem.get(cmdt_Field));
    }
    System.debug(customMDTMap);
    System.debug('------------getCustomMDTMap-------------');
    return customMDTMap;
}

public static  Map<String,Map<String, Object>> getCustomMDTToMap (String cmd_Api_Name){

    System.debug('++++++++++++getCustomMDTMap+++++++++++++');
    SObjectType cmtType = Schema.getGlobalDescribe().get(cmd_Api_Name);
    Map<String,Schema.SObjectField> mfields = cmtType.getDescribe().fields.getMap();    
    System.debug(mfields);      
    String query = 'SELECT ';
    for(String  fieldName : mfields.keySet()){
        query+=fieldName+' ,';
        System.debug(query);
    }
    query=query.removeEnd(',');
    query+=' FROM '+cmd_Api_Name;
    List<sObject> customMDTList = new List<sObject>();
    try{
        customMDTList = Database.query(query);
    }catch(QueryException ex){
        System.debug(ex);
    }
    System.debug(customMDTList);
    Map<String,Map<String, Object>> customMDTMap = new Map<String,Map<String, Object>>();
    for(sObject cMDTItem : customMDTList)
    {
        Map<String,Object> fieldsMap = (Map<String,Object>) JSON.deserializeUntyped(JSON.serialize(cMDTItem));
        System.debug(fieldsMap);
        System.debug(cMDTItem);
        customMDTMap.put((String)cMDTItem.get('DeveloperName'),fieldsMap);
    }
    System.debug(customMDTMap);
    System.debug('------------getCustomMDTMap-------------');

    return customMDTMap;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.