2

The regular Salesforce community sent me here to ask these questions. I'm pretty new to apex and controllers so I'm not sure if I Googled it correctly so please be gentle if this is a super obvious solution.

I have a list that fetches data from an object in Salesforce. I'm using HEDA so everything is school related.
The parent object is a Course and the comes the Course Offering. As part of the Course Offering, we offer something called 'Study Hall'.

we created a VF page where students can sign up for both 'Course' and 'Study Hall'. For the 'Course' part we created a drop-down that lists all courses, including Study Hall. For the 'Study Hall' part we created a multi-select that only lists the study halls.

My Question is how do I remove every 'course' called 'study hall' from the 'Courses' drop-down?

This is the code for the 'Courses' drop-down:

    public List<SelectOption> getCourses(){
    String year = String.valueOf(Date.today().year());//.substring(2);
    year = 'School Year '+year+'%';

    List<SelectOption> options = new List<SelectOption>();
    List<hed__Course_Offering__c> courseOffering = [SELECT Id, hed__Course__r.Name FROM hed__Course_Offering__c WHERE hed__Term__r.name LIKE :year ORDER BY hed__Course__r.Name ASC NULLS LAST];
    for(hed__Course_Offering__c co : courseOffering){
        options.add(new SelectOption(co.hed__Course__r.Name, co.hed__Course__r.Name));
    }

    return options;
}

and here is how we were able to do the reverse of what I want for Study Hall:

    public List<SelectOption> getStudyHalls(){
    String year = String.valueOf(Date.today().year());//.substring(2);
    year = 'School Year '+year+'%';

    List<SelectOption> options = new List<SelectOption>();
    List<hed__Course_Offering__c> courseOffering = [SELECT Id, hed__Course__r.Name, Name FROM hed__Course_Offering__c WHERE hed__Term__r.name LIKE :year AND hed__Course__r.Name = 'Study Hall'];
    for(hed__Course_Offering__c co : courseOffering){
        options.add(new SelectOption(co.Name, co.hed__Course__r.Name + ' - ' + co.Name));
    }

    return options;
}
  • 2
    Welcome to SFSE and thank you for including relevant information and code in your question. It looks like you can modify your query in getCourses() to include WHERE ... AND hed__Course__r.Name != 'Study Hall' ..., inverting your second snippet. Is that not what you're aiming at? – David Reed Feb 5 '18 at 21:09
  • @DavidReed Thank you so much for your warm welcome and thanks for the solution. I know it was something simple. I've been thinking way to complicated instead of just using != for those in the future who are encountering the same: adding AND hed__Course__r.Name != 'Study Hall' did the job. – Michel Feb 5 '18 at 21:46
1

(Comment to Answer for posterity).

Modifying the query in getCourses() to include the course name in the WHERE clause achieves this objective without any additional Apex:

WHERE ... AND hed__Course__r.Name != 'Study Hall' ...

  • (got it - thanks David) @DavidReed Thank you so much for your warm welcome and thanks for the solution. I know it was something simple. I've been thinking way to complicated instead of just using != for those in the future who are encountering the same: adding AND hed__Course__r.Name != 'Study Hall' did the job. – Michel Feb 7 '18 at 19:47

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.