0

I am trying to debug some code that I found in an org that I joined. We have a test that is failing because of null results. If you look at the code below, there is test data created for student and afterwards student gpas for those students.

However when you try to print the 'Student_GPAs__r', there is no data returned. Is there something wrong with this approach of accessing related fields? Or is it something else?

 // insert 300 students
        for (Integer i = 0;i<300; i++) {
            students.add(new Student__c(Last_Name__c = 'Account'+ i, First_Name__c = 'Test', School_Year__c = '2010-11;2011-12', 
                                          GradeClass__c = '01', School_Orphanage_Org__c = 'AEMSA SM'));
        }
        insert students;
        
        // find the students just inserted. add gpas for each
        for (Student__c stud : [select id from Student__c where First_Name__c = 'Test']) {
            gpas.add(new Student_GPA__c(Year_Taken__c='2010-11', Student__c=stud.id));
        }
        insert gpas;

        System.debug('gpas ' + gpas[0]);

        for(Student__c s: students){
            System.debug('value of Student_GPAs__r' + s.Student_GPAs__r);
           
            for(Student_GPA__c g: s.Student_GPAs__r){
                System.debug('related gpas '+ g);
            }
        }

Attached is the screenshot of the results. results

1
  • 1
    I am assuming this is running from a user who has access to all data. The data seems to be there, you will need to query it after inserting insert gpas, something like: students = [Select Id, <some more fields here>, (Select Id ,<some more fields here> from Student_GPAs__r) FROM Student__c]
    – manjit5190
    Jun 14 at 12:37
0

You can make this simpler (and avoid requerying the Students to add the GPAs)

// insert 300 students
for (Integer i = 0;i<300; i++) {
        students.add(new Student__c(Last_Name__c = 'Account'+ i, First_Name__c = 'Test', School_Year__c = '2010-11;2011-12', 
                                      GradeClass__c = '01', School_Orphanage_Org__c = 'AEMSA SM'));
}
insert students;
 
GPA__c[] gpas = new List<GPA__c>();   
for (Student__c s: students) {    // use students just inserted. add gpas for each
     gpas.add(new Student_GPA__c(Year_Taken__c='2010-11', Student__c=s.id));
}
insert gpas;

// You need to requery with a subsselect to get the relationship records
for(Student__c s: [SELECT ... , (SELECT Year_taken__c
                                   FROM Student_GPAs__r)
                     FROM Student__c
                     WHERE Id IN :students){
        
    for(Student_GPA__c g: s.Student_GPAs__r){
        System.debug('related gpas '+ g);
    }
}
1
  • Thanks @cropredy. This works!
    – Pauline
    Jun 15 at 8:06
1

In this loop...

for(Student__c s : students)

...the code is looping through the students List that was created and inserted before any of the Student_GPA__c data was created.

So while the objects in this list variable did get their id values returned after insertion, they do not hold any reference to any data created after them.

So try changing the loop declaration to look more like the previous loop where the GPAs were created:

for (Student__c s : [select id, <<etc.>> from Student__c where First_Name__c = 'Test'])

Or better yet, create a new List<Student__c> out of that selection and use it in both loops (e.g.,

List<Student__c> studentDataList = [select id, <<etc.>> from Student__c where First_Name__c = 'Test'];
...
// Creating GPAS:
for (Student__c stud : studentDataList) ...
...
// In your System(debug) loop:
for (Student__c s : studentDataList) ...

)

That should help your debugging, to help determine whether you actually have data here, so you can continue looking for what is causing your test failure.

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.