4

I have the following code:

public static void getFields(){

    SObject obj = (SObject) Type.forName('Health_Cloud_Integration_Settings__mdt').newInstance();
    DescribeSObjectResult describe = obj.getSObjectType().getDescribe();
    Map<String, Schema.SObjectField> fieldMap = describe.fields.getMap();

    for (String fieldName: fieldMap.keySet()) {
        //get all the fields label for Account Object
        String fieldLabel = fieldMap.get(fieldName).getDescribe().getLabel();
        //get data types for each fields
        Schema.DisplayType fielddataType = fieldMap.get(fieldName).getDescribe().getType();
        System.debug(fieldLabel + ' | ' + fielddataType);
        // If fieldtype = field definition add api name to list of strings for soql 


}

However field data type returns string for my Field Definition type fields? How do I do this?

3
  • Just be aware that use of global describe and field definition custom metadata type fields are very slow (they use many 100s of milliseconds to process in a given request and this can be a significant issue if you are doing CPU intensive work in that same request). Take a look at this answer for some details on this.
    – Phil W
    Commented Apr 6, 2020 at 16:28
  • Thanks Phil. Really useful to know. I have updated my OP + Code. Commented Apr 6, 2020 at 16:44
  • Looking at the DescribeFieldResult, and confirming that a Field Definition is reported as being a STRING display type, I cannot see any way to detect that this is a Field Definition using the Schema API. Perhaps you could deal with this using a naming convention (so the Field Definition field API names are "*Field__c" for example)?
    – Phil W
    Commented Apr 6, 2020 at 18:50

1 Answer 1

5

Instead of describe calls, query FieldDefinition object.

There is a field named ExtraTypeInfo that has information to indicate it is an externallookup and also the DataType is Picklist()

The below code is tested in my Dev org and return the info .

for(FieldDefinition field: [Select Id, DataType, QualifiedApiName, ExtraTypeInfo from FieldDefinition where EntityDefinition.QualifiedApiName='Health_Cloud_Integration_Settings__mdt']){
   if(field.ExtraTypeInfo != null && field.ExtraTypeInfo=='externallookup' &&  field.DataType == 'Picklist()') {
       system.debug(field.QualifiedApiName);
  }
}

You must log in to answer this question.

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