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.