8

I'm trying to use Custom Metadata Types to specify some relationships between objects. So I started out by creating my Custom Metadata Type and adding a Metadata Relationship to an Entity Definition.

In implementing this approach, I ran into an interesting surprise. When I tried to get an object's describe based on DeveloperName, it didn't give me any data. If I have an object whose API Name is My_Object__c, the DeveloperName field contains My_Object.

MyMetadata__mdt mapping = [
    SELECT SObjectType__c, SObjectType__r.DeveloperName
    FROM MyMetadata__mdt LIMIT 1
];
system.debug(mapping.SObjectType__c); // yields 01I00000000xxxx
system.debug(mapping.SObjectType__r.DeveloperName); // yields My_Object
SObjectType s = Schema.getGlobalDescribe().get(mapping.SObjectType__r.DeveloperName);

Is there no way to get it to return the API Name in a way I can pass directly to describe methods? The same problem applies when I try to query a related Field Definition as well.

17

Both the EntityDefinition and FieldDefinition objects have a field called QualifiedApiName that does what I am looking for:

MyMetadata__mdt mapping = [
    SELECT SObjectType__r.QualifiedApiName
    FROM MyMetadata__mdt LIMIT 1
];
system.debug(mapping.SObjectType__r.QualifiedApiName); // yields My_Object__c
SObjectType s = Schema.getGlobalDescribe().get(mapping.SObjectType__r.QualifiedApiName);

When using this field, I can successfully retrieve describes in a dynamic manner.

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.