0
CustomMetada__mdt metaDataRecord = CustomMetada__mdt.getAll();
for (SObjectField field: metaDataRecord.getSObjectType().getDescribe().fields.getMap().values()) {
    System.debug(field.getDescribe().getName());
    if (field.getDescribe().getType() == Schema.DisplayType.Boolean && field.getDescribe().getName() != 'Name') {
        Boolean fieldValue = (Boolean) metaDataRecord.get(field);//Getting Null
    }
}

This results in a null for one record in the metadata but works perfectly for the rest of the records. Instead, if I query the field and use that to get the field value it works perfectly. Why am I getting a null value when I use getAll from metadata?

1 Answer 1

0

The issue you are experiencing might be due to the way you are accessing the field value using the getAll() method from Custom Metadata.

The getAll() method returns a Map<String, CustomMetada__mdt> where the key is the record name and the value is the corresponding metadata record. However, when accessing the field value using metaDataRecord.get(field), it is expecting the field API name as a string, not the actual SObjectField instance.

To resolve this, you can update your code as follows:

CustomMetada__mdt metaDataRecord = CustomMetada__mdt.getAll();

for (SObjectField field : metaDataRecord.getSObjectType().getDescribe().fields.getMap().values()) {
    System.debug(field.getDescribe().getName());
    if (field.getDescribe().getType() == Schema.DisplayType.Boolean && field.getDescribe().getName() != 'Name') {
        String fieldApiName = field.getDescribe().getName(); // Get the API name of the field

        Boolean fieldValue = (Boolean) metaDataRecord.get('RecordName').get(fieldApiName); // Replace 'RecordName' with the actual record name

        // Use the field value as needed
    }
}

Make sure to replace 'RecordName' with the actual name of the metadata record you want to retrieve the field value from.

This code retrieves the API name of the field using field.getDescribe().getName() and then fetches the corresponding field value using metaDataRecord.get('RecordName').get(fieldApiName).

By providing the field API name as a string, you should be able to retrieve the field value correctly without encountering null values.

3
  • The SObject class does have a .get() method which takes an SObjectField reference (otherwise the code would not have compiled or run).
    – Derek F
    Commented Jun 5, 2023 at 15:45
  • Hey, but I tried both using field.getDescribe().getName() and also a String variable but am still facing the issue. One more thing to be noticed is that I am facing this issue only for one record in metadata and not for the rest of the records. Commented Jun 7, 2023 at 5:26
  • And hey I also could not find a get method in SObject class where we could provide the record name. Please advise me in the right direction if I am wrong Commented Jun 7, 2023 at 6:28

You must log in to answer this question.

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