+ Start a Discussion
Rahul Kumar 652Rahul Kumar 652 

Having some confusion in parent-child SOQL

I try to query the records using parent-child SOQL in test method  but i get the child reocrds for each parent in reverse order , if the query returns more than one parent records .
But if it return a single parent record than the child records are in proper order.

I attach the example code below , please go through it . 
@IsTest static void test(){
        List<Class__c> cList = new List<Class__c>();
        Class__c classRecord1 = new Class__c(Board__c = 'Bihar',Fee__c = 100, MaxSize__c = 4);
        Class__c classRecord2 = new Class__c(Board__c = 'Bihar',Fee__c = 1000, MaxSize__c = 5);
        cList.add(classRecord1);
        cList.add(classRecord2);
        insert cList;

        List<Student__c> studentList = new List<Student__c>();

        for(Integer i = 0; i < 4; i++){
            Student__c studentData1 = new Student__c(First_Name__c = 'Test Name '+ i + ' c0', LastName__c = 'LastName' + i, Sex__c = 'Female',Class__c = cList[0].Id);

            Student__c studentData2 = new Student__c(First_Name__c = 'Test Name '+ i + ' c1', LastName__c = 'LastName' + i, Sex__c = 'Female',Class__c = cList[1].Id);

            studentList.add(studentData1);
            studentList.add(studentData2);
        }

        insert studentList;

        Class__c output = [SELECT MaxSize__c, Fee__c, (SELECT First_Name__c FROM Students__r) FROM Class__c LIMIT 1];
        System.debug('Single record from output ' + output.Students__r);

        List<Class__c> outputList = [SELECT MaxSize__c, Fee__c, (SELECT First_Name__c FROM Students__r) FROM Class__c];
        System.debug('Single record from list of outputs '+ outputList[0].Students__r);
    }
The output for both debug are : 
      For 1st debug  :   Single record from output (Student__c:{Class__c=a062x0000023FrkAAE, Id=a052x000003JrTrAAK, First_Name__c=Test Name 0 c0}, Student__c:{Class__c=a062x0000023FrkAAE, Id=a052x000003JrTtAAK, First_Name__c=Test Name 1 c0}, Student__c:{Class__c=a062x0000023FrkAAE, Id=a052x000003JrTvAAK, First_Name__c=Test Name 2 c0}, Student__c:{Class__c=a062x0000023FrkAAE, Id=a052x000003JrTxAAK, First_Name__c=Test Name 3 c0})

For 2nd Debug :     Single record from list of outputs (Student__c:{Class__c=a062x0000023FrkAAE, Id=a052x000003JrTxAAK, First_Name__c=Test Name 3 c0}, Student__c:{Class__c=a062x0000023FrkAAE, Id=a052x000003JrTvAAK, First_Name__c=Test Name 2 c0}, Student__c:{Class__c=a062x0000023FrkAAE, Id=a052x000003JrTtAAK, First_Name__c=Test Name 1 c0}, Student__c:{Class__c=a062x0000023FrkAAE, Id=a052x000003JrTrAAK, First_Name__c=Test Name 0 c0})

You can see that the output of 1st debug gives student records in proper order as they are inserted , but output of 2nd debug givess student records in reverse order as they are inserted. 

I want to know why this happens . 

Thanks in advance
​​​​​​​
Best Answer chosen by Rahul Kumar 652
Arvind_SinghArvind_Singh
Hello Rahul,
Look like same question answered by me 
https://success.salesforce.com/answers?id=9063A000000liMtQAI

Please marked it solved if it solved issue.

- Arvind

All Answers

Himanshu sharma 284Himanshu sharma 284
I try to query the records using parent-child SOQL in test  today news (https://www.rktechnews.com)  method  but i get the child reocrds for each parent in reverse order , if the query returns more than one parent records .
Arvind_SinghArvind_Singh
Hello Rahul,
Look like same question answered by me 
https://success.salesforce.com/answers?id=9063A000000liMtQAI

Please marked it solved if it solved issue.

- Arvind
This was selected as the best answer