3

I have Custom Metadata types with simple relationships between them. When I query them with a relationship query and try to ORDER BY, only 1 of the inner records is returned.

I run this in Anonymous Apex:

Id tmp = ''; // This is populated, I'm just not putting it here
List<Parent__mdt> tmpList = [SELECT Id, MasterLabel, Custom_Order__c, Super__c,
                             (SELECT Id, MasterLabel, Custom_Order__c,
                              FROM Childs__r) 
                             FROM Parent__mdt 
                             WHERE Super__c = :tmp 
                             ORDER BY Custom_Order__c ASC NULLS FIRST];

System.debug(tmpList[0].Childs__r.size());

...and I correctly see that there are 36 'Child' metadata records within that Parent. However, running the same code with an ORDER BY in the relationship clause:

Id tmp = ''; // Again, this is populated
List<Parent__mdt> tmpList = [SELECT Id, MasterLabel, Custom_Order__c, Super__c,
                             (SELECT Id, MasterLabel, Custom_Order__c,
                              FROM Childs__r
                              ORDER BY Custom_Order__c) 
                             FROM Parent__mdt 
                             WHERE Super__c = :tmp 
                             ORDER BY Custom_Order__c ASC NULLS FIRST];

System.debug(tmpList[0].Childs__r.size());

...returns only 1 'Child' within the Parent.

Is this documented anywhere? Is there a way around this?

  • 1
    There have been a number of known issues this release regarding querying custom metadata... You may want to log a bug with Support. – sfdcfox Feb 22 '19 at 20:44
  • 1
    the workaround would be to use a custom apex type that implemented Comparable interface and sort the children in code – cropredy Feb 22 '19 at 23:59
  • @sfdcfox Thanks for the heads-up, I'll log with support when I get the chance. – iwhitt567 Feb 25 '19 at 15:27
  • @cropredy Thanks for pointing me toward the comparable interface, that's going to make things a lot smoother. – iwhitt567 Feb 25 '19 at 15:28
0

You can order by with only non relationship fileds for custom mera datatype as per documentation. Check out this link https://resources.docs.salesforce.com/204/latest/en-us/sfdc/pdf/custom_metadata_types_impl_guide.pdf

| improve this answer | |

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.