function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sp13sp13 

save multiple records at a time

i have a problem saving multiple records at a time. i think the problem is in the saveSeats() i just don't get it. the data display fine in the pageblocktable but it doesn't save when i click the save button. any idea what's the problem here? please help.

Apex Class:
public with sharing class NewScheduleSeatsCX {
    List<Seat__c> seatList = new List<Seat__c>();    //saving seat records
    public List<SeatsWrapper> wrappers {get;set;}        //wrapperlist
    public List<Integer> numberOfSeats {get;set;}
   
    public Schedule__c sd {get;set;}
    public Schedule__c schedule {
      get {
        if (schedule == null)
          schedule = new Schedule__c();
        return schedule;
      }
      set;
    }

    public NewScheduleSeatsCX(ApexPages.StandardController controller) {
        wrappers = new List<SeatsWrapper>();
        numberOfSeats = new List<Integer>();
      
        sd=[select id, name, Number_Of_Seats__c from Schedule__c where id =: apexPages.currentPage().getParameters().get('id')];
        for(integer i=1;i<=sd.Number_Of_Seats__c; i++){
            SeatsWrapper temp = new SeatsWrapper();
            temp.seatNum = i;
            temp.seat = new Seat__c();
            wrappers.add(temp);
        }
    }
  
/**************    Wrapper    *************/
    public class SeatsWrapper {
 public Seat__c seat {get;set;}
 public Integer seatNum {get;set;}
    }
/**************    /Wrapper    *************/

//------------------------------   Save Seats     ------------------------------//
    public pageReference saveSeats() {
        try {
            for(SeatsWrapper  s : wrappers ) {
                    Seat__c newSeats = new Seat__c();
   
                    String seatName = newSeats.Name;
                    Integer sN = Integer.valueOf(seatName);
                   
                    sN = s.seatNum;
                    newSeats.Schedule__c = schedule.Id;
                    newSeats.Taken__c = s.seat.Taken__c;
                   
                    seatList.add(newSeats);
                }
                insert seatList;            //save all seats
          
        } catch(Exception e) {
            apexPages.addMessages(e);
        }
        return null;
    }
//------------------------------   /Save Seats     ------------------------------//
}

VF Page:
               <apex:pageBlockTable value="{!wrappers}" var="sched">
                         <apex:column headerValue="Seat #" style="padding: 0px 20px;">
                                     <apex:outputText value="{!sched.seatNum}"/>
                         </apex:column>
                         <apex:column headerValue="Taken" style="padding: 0px 20px;">
                                     <apex:inputField value="{!sched.seat.Taken__c}"/>
                         </apex:column>
                </apex:pageBlockTable>

srinu_SFDCsrinu_SFDC
Did you enclose <apex:pageBlockTable> in <apex:form>?
sguptasgupta
Try this..
<pre>
for(SeatsWrapper  s : wrappers ) {
                    Seat__c newSeats =s.seat;
                    newSeats.Schedule__c = schedule.Id;
                    
                    seatList.add(newSeats);
                }
                insert seatList;            //save all seats


</pre>
Craig WarheitCraig Warheit
In your code, I don't see where you're populating the wrapper.

In this block:

/**************    Wrapper    *************/
public class SeatsWrapper {
public Seat__c seat {get;set;}
public Integer seatNum {get;set;}
/**************    /Wrapper    *************/
}


there should be something like this:


/**************    Wrapper    *************/
public class SeatsWrapper {
public Seat__c seat {get;set;}
public Integer seatNum {get;set;}

public cSeatsWrapper(Seat__c s, Integer i) {
  seat = s;
  seatNum = i;
}
}
/**************    /Wrapper    *************/
sp13sp13
@srinu_SFDC: yes the pageBlockTable in inside the apex:form
@sgupta: it didn't work
@Craig Warheit: when i tried to add the
public SeatsWrapper(Seat__c s, Integer i) {
               seat = s;
               seatNum = i;
}

 it shows this error: Error: NewScheduleSeatsCX Compile Error: Constructor not defined: [NewScheduleSeatsCX.SeatsWrapper].<Constructor>() at this line: SeatsWrapper temp = new SeatsWrapper(); inside the constructor. what is the problem here?

thank you all for helping

srinu_SFDCsrinu_SFDC
sp13,

Since the default constructor is overriddden you need to pass the arguments while object creation:

SeatsWrapper temp = new SeatsWrapper(new Seat__c(), i);