It seems that this issue only occures if the for-each-loop handles more than one entry. If the
SELECT-Statement just handles one
Near_By_Metadata__mdt, everything works fine and i get all entries from the
Near_By_Table_Fields_Metadata-Subquery in the right order. In my example, all three (one marked as header, two not marked).
But if the for-each-loop handles a second entry, the described problem occures. The second entry has the following
Near_By_Table_Fields_Metadata: one marked as header and three not marked.
I have some strange behaviour with one of my SOQL-Queries and i have absolutly no idea why this happens. Maybe someone here is able to help me.
I have the following for-each-loop with the query:
for(Near_By_Metadata__mdt nb : [SELECT Id, List_Overview_Header_API_Name__c, Object__r.QualifiedAPIName , Longitude_Field_API_Name__c, Latitude_Field_API_Name__c, Geolocation_Field__r.QualifiedAPIName, Marker_Color__c, LDS_Icon_Name__c, Additional_List_Info_Field_API_Name__c, (SELECT Field__r.QualifiedAPIName FROM Near_By_Detail_Fields_Metadata__r), (SELECT Field__r.QualifiedAPIName, Is_Header__c FROM Near_By_Table_Fields_Metadata__r ORDER BY Is_Header__c DESC) FROM Near_By_Metadata__mdt WHERE Is_Active__c = TRUE AND Object__r.QualifiedAPIName IN: objectsToSearchList])
The problem here ist the last subquery:
(SELECT Field__r.QualifiedAPIName, Is_Header__c FROM Near_By_Table_Fields_Metadata__r ORDER BY Is_Header__c DESC)
If i try, later in the code, to access all
Near_By_Table_Fields_Metadata__r by using
nb.Near_By_Table_Fields_Metadata__r it seems that salesforce discards or "forgot" some entries.
I have three entries of
Near_By_Table_Fields_Metadata on one single
Near_By_Metadata__mdt record. One with
Is_Header__c set to TRUE and two set to FALSE.
If i am using the above query and trying to access the List i only get one entry. The one that is active. If i trying to
ORDER BY ASC i got two entries. The two with the flag set to FALSE. And if i am removing the the whole
ORDER BY, i got all values.
Does anyone has an idea what the problem could be here and how to solve it?