If i have a master-detail relationship with a "Classroom" and a "Student" Object. Say the master object is a classroom and the child objects are students. How do i aggregate a list of the student's names in apex given the following model?

Classroom__c Object

  • Name
  • Id
  • Subject

Student__c Object

  • Name
  • Id
  • Phone number
  • Email

Would one just utilize soql? Or just a trigger and update a field record?

I would like to aggregate the students names into a list for an email alert.

List<Student__c> studentList = [SELECT Name, Classroom__r.id FROM Student__c WHERE Classroom__r.id =: yourClassroomId];
List<String> studentNames = new List<String>();
for (Student__c s : studentList){


  • 2
    You're missing a for loop and you can't assign query results to List<String>.
    – Adrian Larson
    May 28 '15 at 14:13

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.