function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
el hadri oussamael hadri oussama 

Junction object join soql help needed

Hello ,
I need help to be build a join query for a junction object , I had a junction object  StudentCourse__c that join Member__c and Course__c  Objetcs , I want to be able to select a list of studentCourse based on a list of members and courses .

the list of members : 
students= [select id, Name,FirstName__c from Member__c where 
                   id  in(select Student__C from StudentCourse__c where Course__r.Year__c= : schoolYear ) 
                   and School__c=: school and  Grade__c= :gradeclasse];

the list of courses: 
courses= [select id ,Name ,Scale__c ,Subject_Name__c from Course__c where Grade__c= : gradeclasse and School__c= :school];
to retrive one record it's easy you just call the query below but the problem is I am exceeding the limits when
I loop membres or courses  so I need a way to retrieve all the StudentCourses  whitout using a loop.
StudentCourse__c  studentCourse = [select id   from StudentCourse__c where Course__c= :course.ID and Student__c= :student.ID];

Thank you in advance
Shun KosakaShun Kosaka
Hello,
You can use a list of studentCourse instead of using a loop. 
List<StudentCourse__c> studentCourseList = [select id from StudentCourse__c where YOUR CONDITION HERE];

for(StudentCourse__c studentCourse : studentCourseList){
//Do Something here
}
or,
for(StudentCourse__c studentCourse :  [select id from StudentCourse__c where YOUR CONDITION HERE]){
//Do Something here
}
When you retrieve over 50000 records, @ReadOnly annotation is required.

See also,
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for_SOQL.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_ReadOnly.htm