Winter 19 release provides ability to instantiate the custom metadata in apex .This seems to be useful to generate data for test classes .

However when we have metadata relationship entity or field entity relationship the QualifiedName comes as null in Test class .

Lets take an example with the below code in a Test class .For now use execute Anonymous via Dev console

Relationship__mdt relationship = new Relationship__mdt(
        DeveloperName = 'Test',
        Active__c = true,
        Object__c = 'Account',
        AccountRelationship__c = 'ParentId'

In the above code Object__c is of type metadataRelationship (Object definition ) and AccountRelationship__c is also of metadataRelationship type (Field Entity Definition) .

When i do relationship.Object__r.QualifiedAPIName in test context I receive null and not 'Account'.

Similarly when i do relationship.AccountRelationship__r.QualifiedAPIName in test context I receive null and not ParentId .

This makes it not helpful again for use cases where metadata data has entity relationship and the code uses QualifiedName .

Is it a bug or I am overlooking something ?

  • I've never observed this problem. What instance are you on?
    – Adrian Larson
    Nov 20 '18 at 16:03
  • Sandbox instance and note this issue is in test class Nov 20 '18 at 16:04
  • No I mean cs10, cs20, etc. Also are you actually able to construct a record or is that just pseudo-code to illustrate?
    – Adrian Larson
    Nov 20 '18 at 16:06
  • 6
    relationships are not automatically populated, you have to query for them. Also, all the fields you need to use are read-only, so you would have to either create a big JSON string to fake the relationship, or you'll have to use the stub API/dependency injection.
    – sfdcfox
    Nov 20 '18 at 16:17
  • 3
    Last I checked you could not even construct Custom Metadata records. This is the first I've seen of that change.
    – Adrian Larson
    Nov 20 '18 at 16:20

You still need to use JSON construction if you want to set parent attributes without a true query for the records.

Non Functional Code

EntityDefinition sourceObject = [SELECT QualifiedApiName FROM EntityDefinition LIMIT 1];
MyMapping__mdt mapping = new MyMapping__mdt(
    Source_Object__r = sourceObject


Field is not writeable: MyMapping__mdt.Source_Object__r

Functional Code

Map<String, Object> mockMapping = new Map<String, Object>
    'attributes' => new Map<String, Object>
        'type' => 'MyMapping__mdt '
    'Source_Object__r' => new Map<String, Object>
        'attributes' => new Map<String, Object>
            'type' => 'EntityDefinition'
        'QualifiedApiName' => 'Account'
MyMapping__mdt mapping = (MyMapping__mdt)JSON.deserialize(
    JSON.serialize(mockMapping), MyMapping__mdt.class



  • 1
    Thanks for the response .I just feel this should be natively supported Nov 20 '18 at 21:19
  • 3
    Perhaps it will come in future releases. It was a pretty big change just to allow construction of Custom Metadata at all.
    – Adrian Larson
    Nov 20 '18 at 21:45

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.