You need to sign in to do that
Don't have an account?
sp13
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 NewScheduleSeatsCX(ApexPages.StandardController controller) {
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>
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 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);
}
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 {
/************** Wrapper *************/
public class SeatsWrapper {
public Seat__c seat {get;set;}
public Integer seatNum {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 ------------------------------//
}
/************** /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>
<pre>
for(SeatsWrapper s : wrappers ) {
Seat__c newSeats =s.seat;
newSeats.Schedule__c = schedule.Id;
seatList.add(newSeats);
}
insert seatList; //save all seats
</pre>
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 *************/
@sgupta: it didn't work
@Craig Warheit: when i tried to add the
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
Since the default constructor is overriddden you need to pass the arguments while object creation:
SeatsWrapper temp = new SeatsWrapper(new Seat__c(), i);