function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kevin GallKevin Gall 

Querying Metadata Relationships in Custom Metadata Types

I am new to Salesforce development and am trying to write a configurable Apex class using Custom Metadata Types. On the surface, it seems perfect, because what I need to do is associate fields from one object with fields from another object to indicate to my Apex class that it should copy data from one field directly into the other on two separate object types. (Please don't suggest that they should be the same object type. They can't be for org-specific reasons.)

The Metadata Relationships field type allows a user configuring my class to choose an object and a field from that object, not type in a class name and field name which my code assumes is correct to use in an sObject.get() or .put() method call. I planned on using this configured information to discover which objects to relate, grab the value of a field for a concrete record of one object type and copy it to a field in a new record of the other object type.

So I created the Custom Metadata Type and created a couple test records to test my queries, but instead of returning useful sObject or other Schema-related information, the Metadata Relationship fields simply return the Salesforce ID for the relevant object or field type. Note: this is an ID for a Metadata record, not for a Data record.

"OK," I thought, "I can use this."
...Apparently not. It seems impossible to query an sObject type using its ID, which I assume is because it is Metadata, so it's not supported the same way as if I would query a data record.

Fine.

I looked at the Metadata API and thought I'd create a set of wrapper Apex classes around the SOAP web services, basically doing a web callout from within an Apex Class to my Metadata API within my org. The simplest way to do this is supposed to be to download the WSDL for the Metadata API, then "Generate Apex Classes from WSDL" in Apex Class definitions in Setup.

However, whenever I try to import, it tells me there are class name conflicts for the "Metadata" complexType. I've renamed that complex type to something guaranteed org-unique, and searched through the WSDL to find multiple complex type that maybe mistakenly redefined "Metadata," but nothing. And it can't import from WSDL as long as that issue occurs.

So that's everything I've tried. I'm sure I'm missing something somewhere, but I've spent many hours poring through documentation and keep hitting road blocks. How can I use the Metadata Relationships fields within an Apex Class to make my app written in Apex configurable?
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Kevin,

May I request you to please check the below link for reference. I hope it will be helpful.

Best Regards
Rahul Kumar
Kevin GallKevin Gall
Hi Rahul,

Unfortunately, those docs aren't helpful for my question. I read them about 20 times before posting.

I think the answer might be in the Tooling API. The EntityDefinition object was queryable from an Anonymous Apex Execution window and provided me with the object name for one of my specified objects. Haven't tested fields, or an actual class context (if that's a different runtime?).