1

I am trying to put visualforce page on a custom object's standard page layout. I want to display all the courses that have been assigned to the student. I have three custom objects Student, StudentCourseDtlAssc, and Course Detail. I want to display the courses that the student is taking on the Student page layout. Course Detail have information about the course such as name, time, room, etc. StudentCourseDtlAssc is a junction object between student and course detail. Course Detail and StudentCourseDtlAssc have a Master-Detail Relationship. StudentCourseDtlAssc and Student has a lookup relationship.

Controller Code:

public class StudentCourseList {
    private ApexPages.StandardController controller {get; set;}
    private Students__c s;
    public StudentCourseList(ApexPages.StandardController controller){
    this.controller = controller;
    this.s = (Students__c)controller.getRecord();
    }
    public List<StudentCourseDtlAssc__c> CourseDetails;
    public List<StudentCourseDtlAssc__c> getCourseDetails(){
        List<StudentCourseDtlAssc__c> courses = Database.query(
        'SELECT StudentCourseDtlAsscs__c.Course_Detail__r.FKCourse__r.Name,' +
        'StudentCourseDtlAsscs__c.Course_Detail__r.DOW_Abbr__c,StudentCourseDtlAsscs__c.Course_Detail__r.Professor__c,StudentCourseDtlAsscs__c.Course_Detail__r.FKCourse__r.Name' +
        'StudentCourseDtlAsscs__c.Course_Detail__r.StartDate__c FROM StudentCourseDtlAssc__c');
        return courses;
    }
}

Visualforce Page:

<apex:page standardController="Students__c" extensions="StudentCourseList">
    <apex:form >
        <apex:pageBlock title="Student Schedule" id="student_schedule">
            <apex:pageBlockTable value="{!CourseDetails}" var="st">
                <apex:column value="{!st.Course_Detail__r.FKCourse__r.Name}"/>
                <apex:column value="{!st.Course_Detail__r.Professor__c}"/>
                <apex:column value="{!st.Course_Detail__r.FKRoom__r.Name}"/>
                <apex:column value="{!st.Course_Detail__r.DOW_Abbr__c}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

I am getting the following error:

Content cannot be displayed: Didn't understand relationship 'StudentCourseDtlAsscs__c' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.

I tried replacing 'StudentCourseDtlAsscs__c' to 'StudentCourseDtlAsscs__r', but I still get a similar error.

Am I headed in the right direction? If so, how do I solve this? Is there another way?

2
  • Have you replaced all occurrences of StudentCourseDtlAsscs__c with __r? Jul 27, 2015 at 1:21
  • Wouldn't you want to write it: StudentCourseDtlAsscs__r.Course_Details__r ? This is assuming that the default relationship names are being used. Jul 27, 2015 at 1:23

2 Answers 2

1

Peter

I believe you are trying to get the Course details for a particular Student.

So instead of querying from the junction object, you can directly query the course detail object based on the Ids you get from the junction object.

Here's a rough SOQL, you might need to adjust the field names as per your actual field names.

List<StudentCourseDtlAssc__c> courses = 
[SELECT FKCourse__r.Name, FKRoom__r.Name, DOW_Abbr__c, Professor__c, StartDate__c
 FROM Course_Detail__c
 WHERE ID IN 
 (SELECT Course_Detail__c FROM StudentCourseDtlAsscs__c WHERE Students__c := s.Id)]
0

You should get rid of the StudentCourseDtlAsscs__cs in front of the fields in your query or keep them as __c since that is the name of the object you are querying, and not the name of a relationship on that object. The problem with your query is likely that you are missing a comma between StudentCourseDtlAsscs__c.Course_Detail__r.FKCourse__r.Name and StudentCourseDtlAsscs__c.Course_Detail__r.StartDate__c.

You also have the same field listed twice in your query, getting the course name twice and not getting the room name.

I believe the following should work:

List<StudentCourseDtlAssc__c> courses = [SELECT Course_Detail__r.FKCourse__r.Name, Course_Detail__r.DOW_Abbr__c, Course_Detail__r.Professor__c, Course_Detail__r.FKRoom__r.Name, Course_Detail__r.StartDate__c FROM StudentCourseDtlAssc__c];

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .