1

Is it possible to use Custom Metadata Types in the subqueries? If yes, how?

In my case, there is a custom metadata type named Custom Actions and there is a Next_Action field in it. When I query for action, I want to also get the date of the next action as well.

What I am trying (and not working) to do is,

SELECT Next_Action__c, 
       Action_Date__c, 
       ( SELECT Action_Date__c, 
                Action_Name__c 
           FROM Custom_Action__mdt
         WHERE Action_Name__c=Next_Action__c
       ) 
  FROM Custom_Action__mdt
WHERE DeveloperName = :currentAction

or should I just go for two seperate SOQLs?

Custom_Action__mdt currentAction = [SELECT Next_Action__c, ... FROM Custom_Action__mdt WHERE WHERE DeveloperName = :currentAction];
Custom_Action__mdt nextAction = [SELECT Action_Date__c, ... FROM Custom_Action__mdt WHERE WHERE DeveloperName = :currentAction.Next_Action__c];

1 Answer 1

1

Custom metadata types cannot have self relationships. So, you cannot have inner query the way you need.

Option 1:

Since custom metadata types are stored in cache after the first read and as generally the number of records will be limited (non-transactional data), you can fetch all records and store in Map<String,Custom_Action__mdt> where key is the Next_Action__c. This would appear a little more elegant compared to 2nd option

Option 2:

What you posted as:

Custom_Action__mdt currentAction = [SELECT Next_Action__c, ... FROM Custom_Action__mdt WHERE WHERE DeveloperName = :currentAction];
Custom_Action__mdt nextAction = [SELECT Action_Date__c, ... FROM Custom_Action__mdt WHERE WHERE DeveloperName = :currentAction.Next_Action__c];

You must log in to answer this question.

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