How can I implement insert operation using map collection instead of list?

I've created a conference scheduling app wherein upon selecting a certain student from a picklist, a table containing multiple objects must be displayed. This table is composed of five columns: Teacher (1st col.), Subject (2nd col.), Room (3rd col.), Time (4th col.), and Availability (5th col.). Fetching all the teachers (from Teacher Set, another object) with corresponding subjects, rooms (both are fields from Teacher object), Time (from PTC Schedule object), and Availability is okay, but there's a problem in saving records to Salesforce. Every time I click the proceed button after the app has confirmed the availability of a teacher for a certain time, values selected are not inserted properly in Salesforce. Selected picklist items are not inserted at all. Aside from this, Teacher and Subject fields are always saved with first records.

Meanwhile, here's my controller and vf page:

vf page:

<apex:page controller="SchedulerCX" sidebar="false" showHeader="false" >

  <apex:form >
    <apex:pageBlock >
      <apex:outputLabel style="margin-left: 55px;">SMITH, JOHN</apex:outputLabel>

  <apex:form >
    <apex:pageBlock id="chooseStudentPageBlock" >            
      <apex:outputText value="{!selectedSId}"></apex:outputText>
      <apex:outputLabel>Select Student:</apex:outputLabel>
      <apex:selectList value="{!selectedSId}">             
        <apex:selectOptions value="{!sNames}"></apex:selectOptions>
        <apex:actionSupport event="onchange"  action="{!fetchTeacherInfo}" reRender="teacherBlock"/>

    <apex:pageBlock id="teacherPageBlock">    
      <apex:pageBlockTable value="{!WrapTS}" var="item" id="teacherBlock">
        <apex:column headerValue="Teacher" value="{!item.t1.First_Name__c} {!item.t1.Last_Name__c}"/>
        <apex:param id="teacherid" name="teacherId" value="{!item.t1.Id}" assignTo="{!selectedTea}"/> 
        <apex:column headerValue="Subject" value="{!item.t1.Subject__c}"/>
        <apex:column headerValue="Room" value="{!item.t1.Room_Assignment__c}"/>
        <apex:column headerValue="Time">
        <apex:selectList size="1" value="{!item.selectedTime}">
          <apex:actionSupport event="onchange"  action="{!checkTeacherSched}" reRender="statusCol"/>
          <apex:selectOption itemValue=""></apex:selectOption>
          <apex:selectOption itemValue="9:00AM - 10:00AM"></apex:selectOption>
          <apex:selectOption itemValue="10:00AM - 11:00AM"></apex:selectOption>
          <apex:selectOption itemValue="11:00AM - 12:00PM"></apex:selectOption>
          <apex:selectOption itemValue="12:00PM - 1:00PM"></apex:selectOption>
          <apex:selectOption itemValue="1:00PM - 2:00PM"></apex:selectOption>
          <apex:selectOption itemValue="2:00PM - 3:00PM"></apex:selectOption>
          <apex:selectOption itemValue="3:00PM - 4:00PM"></apex:selectOption>
          <apex:selectOption itemValue="4:00PM - 5:00PM"></apex:selectOption>
          <apex:param value="{!item.selectedTime}" assignTo="{!objsched.selectedTime}"/>

        <apex:column headerValue="Availability" id="statusCol">
        <!-- <apex:outputText value="{!item.strCheck}">  </apex:outputText> -->
          <apex:image url="{!URLFOR($Resource.cross)}" width="20" height="20" rendered="{!IF(item.strCheck ='Time is no longer available',true,false)}"/>
          <apex:image url="{!URLFOR($Resource.check)}" width="20" height="20" rendered="{!IF(item.strCheck ='AVAILABLE',true,false)}"/> 


  <apex:form >         
    <apex:pageBlock >    
      <apex:commandButton value="Save" action="{!save}"/>
  </apex:form >                 



public with sharing class SchedulerCX{

    public string selectedSId{get;set;}
    public string teacherSet{get;set;}   
    public List<WrapperClass> saveWrapTS{get;set;}
    public List<WrapperClass> wrapTS{get;set;}
    public SET<Id> t2{get;set;}
    public list <String> checker = new list<string>();

    public set<ID> teachers = new set<ID>();
    public set<ID> selectedTId = new set <ID>();
    public set<string> qryTime= new set<string>();
    public List<PTC_Schedule__c> new_schedule = new List<PTC_Schedule__c>();  
    public map<Id, Student__c> mapTeacherIdToStudent = new map<Id, Student__c>();

    public List<selectOption> getSNames(){

        List<selectOption> options = new List<selectOption>(); 
        options.add(new selectOption('--None--','--Select Student--'));
        for(Parent__c p : [Select Id, Name, Last_Name__c, (Select Id, First_Name__c, Last_Name__c from Students__r) 
           from Parent__c
           WHERE Last_Name__c = 'Smith']){
               for(Student__c stud : p.Students__r){ 
                   selectedSId = stud.Id;   
                   options.add(new selectOption(stud.Id, stud.First_Name__c + ' ' + stud.Last_Name__c ));
           return options;


    public SchedulerCX(){

        mapTeacherIdToStudent = new map<Id, Student__c>();
        selectedSId = '--None--';
        selectedTId = new SET<ID>();
        wrapTS = new List<WrapperClass>();
        new_schedule = new List<PTC_Schedule__c>();
        t2 = new SET<Id>();


    public class WrapperClass{
        public Teacher_Set__c ts1{get; set;}
        public Teacher__c t1{get; set;}
        public PTC_Schedule__c p1{get; set;}
        public Parent__c pa1{get;set;}
        public Student__c s1{get; set;}
        public List<Teacher__c> teacherlist{get; set;}
        public List<Student__c> studentlist{get;set;}
        public List<Parent__c> parentlist{get;set;}
        public String strCheck{get;set;}
        public string selectedTime{get;set;}

        public WrapperClass(Teacher__c t2){

            t1 = t2;


        public WrapperClass(PTC_Schedule__c p2){

            p1 = p2;

        public WrapperClass(Student__c s2){
            s1 = s2;

        public WrapperClass(Teacher_Set__c ts2){
            ts1 = ts2;
            teacherlist = ts2.Teachers__r;
            studentlist = ts2.Students__r;


    public void fetchTeacherInfo(){

        wrapTS = new List<WrapperClass>();
        saveWrapTS = new List<WrapperClass>();
        new_schedule = new List<PTC_Schedule__c>();

            for (Student__c stud : [Select Id, Name, First_Name__c, Parent__c, Teacher_Set__c 
                from Student__c WHERE Id = : selectedSId]){   
                    teacherSet = stud.Teacher_Set__c;
            for (Teacher__c myTeacher : [Select Id, Name, First_Name__c, Teacher_Set__c, Middle_Name__c, Last_Name__c 
                from Teacher__c WHERE Teacher_Set__c = : teacherSet]){                  

            for(Teacher__c t : [select id, Name, First_Name__c, Last_Name__c, Teacher_Set__c, Subject__c , Room_Assignment__c 
                from Teacher__c  where Id = : t2]){
                   wrapTS.add(new WrapperClass(t));

    public void checkTeacherSched(){

            for(PTC_Schedule__c objsched1 : [Select Id, Name, Teacher__c, Scheduled_Time__c, Student__c, Parent__c, Teacher_Name__c 
                from PTC_Schedule__c WHERE Teacher__c =: t2 ]){

            for(WrapperClass objsched: wrapTS){ 

                    objsched.strCheck ='Time is no longer available';
                else if (objsched.selectedTime == null){

                    objsched.strCheck = '';                  
                    objsched.strCheck = 'AVAILABLE';


    Public void save(){ 

        for (Student__c teaSet: [Select Id, Name, First_Name__c, Parent__c, Teacher_Set__c 
              from Student__c WHERE Id = : selectedSId]){   
                  mapTeacherIdToStudent.put(teaSet.Teacher_Set__c, teaSet);         


        for(Id teacherId: mapTeacherIdToStudent.keySet()){

        for (Teacher_Set__c tset : [Select Id,Name,
                               (Select Id, Name, First_Name__c, Subject__c from Teachers__r WHERE id =:t2 LIMIT 1 ),
                               (Select Id, Name, First_Name__c, Parent__c from Students__r WHERE Id =: SelectedSId LIMIT 1) 
                                     from Teacher_Set__c WHERE Id =: teachers ]){    
        saveWrapTS.add(new WrapperClass(tset));         


        for(WrapperClass objsched: saveWrapTS){ 
            objsched.p1 = new PTC_Schedule__c();

            objsched.p1.Scheduled_Time__c = objsched.selectedTime;           
            objsched.p1.Student__c = objsched.studentlist[0].Id; 
            objsched.p1.Teacher__c = objsched.teacherlist[0].Id; 
            objsched.p1.Parent__c = objsched.studentlist[0].Parent__c;

        for(WrapperClass thisSched : saveWrapTS){ 
            PTC_Schedule__c newSchedule = new PTC_Schedule__c(); 
                newSchedule.Scheduled_Time__c = thisSched.p1.Scheduled_Time__c; 
                newSchedule.Student__c = thisSched.p1.Student__c; 
                newSchedule.Teacher__c = thisSched.p1.Teacher__c; 
                newSchedule.Parent__c = thisSched.p1.Parent__c; 



        INSERT new_schedule;



All of these fields must be saved in just one object, which is the PTC Schedule custom object.

Any help would be appreciated, thanks.

  • you might want to indent your VF page better so it doesn't scroll off the viewport to the right. Use the edit link
    – cropredy
    Apr 20, 2016 at 1:38

1 Answer 1


Both of the queries in your save method will only ever return 1 value. What follows is some large guesswork.

Your first for loop should just be a statement querying Student__c to select the single one by ID.

Your second for loop should be splitting the string inside Teacher_Set__c by whatever the separator is (';' for multi-select pick lists).

The where clause in your 3rd for loop should be WHERE Id IN :teachers (teachers should also probably be a local variable I think).

There may be more issues... this is just what stood out to me. Hopefully this helps.

You must log in to answer this question.

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