3

I have a object, Student, and I want to display a list of courses the student is taking. I have a junction object between Student and Courses called StudentCourseDtlAssc__c.

I am showing the list on the student page (see below): Student Page

The list below shows every entry in the StudentCourseDtlAssc__c object. I want to show only the coures assigned to, in this case, Charlie Brown.

<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 >
                    <apex:commandLink action="{!deleteCourse}" value="Del" id="commandLinkDelete" >
                        <apex:param name="studentcourse_id" value="{!st.id}" assignTo="{!studentcourse_id}"/>
                    </apex:commandLink>
                </apex:column>
                <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:column value="{!st.Course_Detail__r.StartDate__c}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Here is my controller

public class StudentCourseList {
    private ApexPages.StandardController controller {get; set;}
    public string studentcourse_id {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 StudentCourseDtlAssc__c.Course_Detail__r.FKCourse__r.Name,'+
        'StudentCourseDtlAssc__c.Id, ' +
        'StudentCourseDtlAssc__c.Course_Detail__r.DOW_Abbr__c,' +
        'StudentCourseDtlAssc__c.Course_Detail__r.Professor__c,' + 
        'StudentCourseDtlAssc__c.Course_Detail__r.FKRoom__r.Name,' +
        'StudentCourseDtlAssc__c.Course_Detail__r.StartDate__c FROM StudentCourseDtlAssc__c ' + 
        '');
        return courses;
    }

    public PageReference deleteCourse() {
        List<StudentCourseDtlAssc__c> deleteObj = [SELECT Id FROM StudentCourseDtlAssc__c WHERE StudentCourseDtlAssc__c.Id =:studentcourse_id];
        delete deleteObj;
        return null;
    }
}

I tried to write my query as

'SELECT StudentCourseDtlAssc__c.Course_Detail__r.FKCourse__r.Name,'+
            'StudentCourseDtlAssc__c.Id, ' +
            'StudentCourseDtlAssc__c.Course_Detail__r.DOW_Abbr__c,' +
            'StudentCourseDtlAssc__c.Course_Detail__r.Professor__c,' + 
            'StudentCourseDtlAssc__c.Course_Detail__r.FKRoom__r.Name,' +
            'StudentCourseDtlAssc__c.Course_Detail__r.StartDate__c FROM StudentCourseDtlAssc__c WHERE StudentCourseDtlAssc__c.Student__r.Id =:s.Id'

I get an error:

Variable does not exist: s.Id

How do I pass the student id so I can query only course to that student?

1 Answer 1

1

You can't use an sObject binding within a dynamic SOQL query. Try assigning that to another variable and then reference it:

Id sId = s.Id;
query = 'YourQuery WHERE StudentCourseDtlAssc__c.Student__r.Id = :sId';

You must log in to answer this question.

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