0

There is a query which has to be reduced because it is a bad written code. We can reduce it to a certain level using getAll() method. There is a field C inside the metadata whose datatype is metadata relationship(assume it as metadata B). I am able to fetch all the fields from the metadata A but I am not able to fetch the fields which are inside metadata B. Is there a efficient way to fetch the data using get methods or using loops or using map values. Any help is appreciated.

1 Answer 1

0

The EntityParticle metadata relationship fields are not fetched when using getAll() (as of V60). - Exception - you can fetch MyEntityParticle__r.DeveloperName

I confirmed with this Apex using my org

Map<String,MyCMDT__mdt> cmdtByKey = MyCMDT__mdt.getAll();
System.debug(LoggingLevel.INFO,cmdtByKey);
for (String key: cmdtByKey.keySet()) {
    System.debug(LoggingLevel.INFO,key + ' ' +
                 cmdtByKey.get(key).Entity_particle__r.DeveloperName) + ' ' +
                 cmdtByKey.get(key).Entity_particle__r.QualifiedApiName);
}

Debug output:

11:58:51.25 (57060993)|USER_DEBUG|[4]|INFO|Key1 DevName1 null
11:58:51.25 (57094614)|USER_DEBUG|[4]|INFO|Key2 DevName2 null

The metadata relationship fields are fetched when included in a SOQL query and then you can run Apex for loop filters on the results.

Since SOQL on CMDT does not burn any SOQL, there is no cost to your transaction Limits to use it

3
  • Hi @cropredy, I used "for" loop to fetch values of both the metadata in 2 different maps while satisfying the where clauses too. Right now, My requirement is to collect both the data in 1 map value so we can use that value later in different scenarios. Our requirement is to eliminate SOQL. That's why I need to work with getAll() Commented Feb 27 at 9:33
  • I think you are stuck -- you have to use SOQL to reference values in the metadata relationships.
    – cropredy
    Commented Feb 27 at 20:19
  • This is what I realised yesterday while doing my analysis. The idea of using getAll() has been removed and now we are back to SOQL :). Thanks a lot tho Commented Feb 29 at 6:52

You must log in to answer this question.

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