I have a parent Metadata Type having a child relationship. SOQL behaves differently in each of the following scenarios:
SELECT ALLFIELDS, (SELECT ALLFIELDS FROM Children__r ORDER BY Order__c) FROM Parent__mdt
- Returns: Incorrect children (some are missing)
SELECT ALLFIELDS, (SELECT ALLFIELDS FROM Children__r) FROM Parent__mdt
- Returns: Correct children
SELECT ALLFIELDS, (SELECT ALLFIELDS FROM Children__r ORDER BY Order__c) FROM Parent__mdt WHERE DeveloperName = 'MyName'
- Returns: Correct children
SELECT ALLFIELDS, (SELECT Id, DeveloperName FROM Children__r ORDER BY Order__c) FROM Parent__mdt
- Returns: Correct children but wrong order!
I have also tried using SOQL for-loop as suggested by sfdcfox but no luck:
for (Parent__mdt p : [SELECT ALLFIELDS, (SELECT ALLFIELDS FROM Children__r ORDER BY Order__c) FROM Parent__mdt])
{
Child__mdt[] childList = new Child__mdt[]{};
for (Child__mdt c : p.Children__r) childList.add(c);
System.debug(childList.size()); // wrong
}
Btw ALLFIELDS is a placeholder for all fields on that object.
This seems to be related to this KB issue, but I could not find the exact problem in the KB. Anybody has any solutions? I want to be able to include all fields in my query and I want to return all records.