I have three custom objects, students, course_detail, and StudentCourseDtlAsscs. The StudentCourseDtlAsscs object is junction object between students and course detail. I want to be able to choose a student from my Student Object and add courses from course detail into StudentCourseDtlAsscs. Is this possible in SF and how?

I have a screenshot of my visualforce page: enter image description here

VF Page:

<apex:page controller="CourseDetailMultiselect">
    <apex:form >  
        <apex:outputLabel value="Student Name" for="students"></apex:outputLabel>
        <apex:selectList id="students" value="{!selectedValue}" size="1" title="Student">
        <apex:selectOptions value="{!students}"></apex:selectOptions>
        <c:MultiselectPicklist leftLabel="Available Contacts"
            rightLabel="Selected Contacts"
        <apex:commandButton action="{!save}" value="Save record"/>
        <apex:commandButton action="Cancel" value="Cancel"/>

Here is my Class:

public class CourseDetailMultiselect {
    //Student Picklist
    public string selectedValue {get;set;}
    public List<SelectOption> students;

    public List<selectOption> getStudents(){
        List<selectOption> options = new List<selectOption>(); 
        options.add(new selectOption('','- None-'));
        for(Students__c student : [Select Id, Name FROM Students__c]){
            options.add(new selectOption(student.id, student.Name));
        return options;

    //Course Detail Custom Picklist
    public SelectOption[] selectedCourseDt { get; set; }
    public SelectOption[] allCourseDt { get; set; }

    public String message { get; set; }

    public CourseDetailMultiselect() {
        selectedCourseDt = new List<SelectOption>();

        List<Course_Detail__c> courses = [SELECT Course_Detail__c.FKCourse__r.Name, StartDate__c, Id FROM Course_Detail__c];    
        allCourseDt = new List<SelectOption>();
        for ( Course_Detail__c c : courses ) {
            string temp = c.FKCourse__r.Name + ' - ' + c.StartDate__c;
            allCourseDt.add(new SelectOption(c.Id, temp));

    public PageReference save() {
        return null;       
  • Means if you select a name of student and more than selected courses than your save button should create no of records according to no of courses.. am i right .....? Jul 26, 2015 at 18:25

1 Answer 1


I hope this will help you

public PageReference save() {

    list<StudentCourseDtlAsscs> objs = new list<StudentCourseDtlAsscs>();

    for(id recId : selectedCourseDt){

        objs.add(new StudentCourseDtlAsscs(student__c = selectedValue , course_detail__c =recId));
    insert objs;

    return null;    

You must log in to answer this question.

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