I'm a complete newbie at salesforce and have spent hours trying to figure this out with no luck (so far)!

The schema is fairly simple: (Objects are bold)

There are Teachers (contact record types) that teach Courses which Students (contact record types) can enroll (Enrollment) on.

I need to display the following:

A list of all Courses | The teachers (Courses can only have 1 teacher) | The number of Students enrolled on that course.

ANY help with the query and/or displaying the aggregated result on a visualforce page will be highly appreciated, Thank you.

  • 1
    what you have tried so far? Aug 3 '17 at 3:27
  • Tried normal SQL but obviously that didn't work out so well. Found SOQL trail and completed that which solved that part of my problem.
    – Saqalain
    Aug 4 '17 at 21:19

Even though you haven't mentioned the exact data model, I'll assume enrollment is an intermediate table that connects students with courses in a M-N relationship.

Given that, you can do something like this to obtain data from each of the tables:

SELECT Id, (SELECT Id, Student__r.Name FROM Enrollments__r) FROM Course__c

And in case you want to aggregate something, just fit in a COUNT or GROUP BY with any changes to fit in.


Thank you for your answer (Javier)

After some research I realised there was a trail on SOQL which I completed and was able to get the results I wanted. I also found the method of displaying aggregated results from a blog.

Below the apex page tag

<apex:form >
    <apex:pageBlock >
        <apex:pageBlockTable value="{! newEnrollments}" var="enrollment">

            <apex:column >
                <apex:commandLink action="{!enrollment['corses']}" 
                                  value=" {!enrollment['corses']}" /> 


Difference is of bracket notation rather than dot notation which would've been enrollment.corses in my case.

Note: Headings for results can be displayed through entering the following under the apex column tag:

 <apex:facet name="header"></apex:facet>

Hopefully this helps someone as it took me quite a while to figure out.

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.