+ Start a Discussion
Vijay Kumar Rebbala 11Vijay Kumar Rebbala 11 

How can i save multiple input checkbox values for multiple records from vfp and insert the data as child records.

Multiple records from Student__c are shown on VFP with inputcheckbox. I want to save the input checkbox values and the Student__c Id's in child records.
//VFP:

<apex:page controller="Testctrl">
    <apex:form >
    <apex:repeat value="{!acctlist}" var="accs" >
        <apex:outputText value="{!accs.Name}"></apex:outputText>
        <apex:inputCheckbox value="{!input}" >
        </apex:inputCheckbox><br/>
    </apex:repeat>
    <apex:commandButton action="{!submit}" value="Submit"/>
    </apex:form>
</apex:page>

//CTRL:

public class Testctrl {
    public list<Student__c> acctlist {get;set;}
    public list<Boolean> input {get;set;}
    public list<Id> stuids;
    public list<Attendence__c> attds = new list<Attendence__c>();
    public Integer s;
     
    public Testctrl(){
    system.debug('************checkbox_inputs*****************'+input);
        acctlist = [SELECT Id, Name FROM Student__c];
        s = acctlist.size();
        system.debug('*************recordcount****************'+s);
        system.debug('*************studentlist****************'+acctlist);
        for(Student__c stid: acctlist){
            stuids.add(stid.id);    
        }
        system.debug('************studentids*****************'+stuids);             
    }
    public void submit(){
        for(Integer i=0;i<s;i++){
             attds.add(new Attendence__c(Absent__c = input[i], Student__c = stuids[i]));
        }
        system.debug('************attendence*****************'+attds);
        insert attds;
    }

 
Best Answer chosen by Vijay Kumar Rebbala 11
Akhil AnilAkhil Anil
Hi Vijay,

Refer the below snippet.

VF page
<apex:page controller="Testctrl">
    <apex:form >
    <apex:repeat value="{!Students}" var="accs" >
        <apex:outputText value="{!accs.sd.Name}"></apex:outputText>
        <apex:inputCheckbox value="{!accs.selected}">
        </apex:inputCheckbox><br/>
    </apex:repeat> 
    <apex:commandButton action="{!submit}" value="Submit"/>
    </apex:form>
</apex:page>

Apex class
 
public class Testctrl {

    List<StudentWrapper> sw {get; set;}
    public list<Attendence__c> attds = new list<Attendence__c>();
    
    class StudentWrapper {
       
       public Student__c sd {get; set;}
       public boolean selected {get; set;}
       
       public StudentWrapper(Student__c st) {
          
          sd = st;
          selected = false;         
       
       }
    
    }
    
    public List<StudentWrapper> getStudents() {
      
      if(sw == null) {
        
        sw = new List<StudentWrapper>();
      
        for(Student__c temp:[Select Id, Name from Student__c]) {
         
           StudentWrapper snew = new StudentWrapper(temp);
           sw.add(snew);
        
        }  
      }  
      
      return sw;
      
    }
    
    public void submit(){
        attds.clear();
        for(StudentWrapper sout:getStudents()){
             attds.add(new Attendence__c(Absent__c = sout.selected, Student__c = sout.sd.Id));
        }
        system.debug('************attendence*****************'+attds);
        insert attds;
    }
}

Kindly mark it as an answer if that works for you.
 

All Answers

Akhil AnilAkhil Anil
Hi Vijay,

Refer the below snippet.

VF page
<apex:page controller="Testctrl">
    <apex:form >
    <apex:repeat value="{!Students}" var="accs" >
        <apex:outputText value="{!accs.sd.Name}"></apex:outputText>
        <apex:inputCheckbox value="{!accs.selected}">
        </apex:inputCheckbox><br/>
    </apex:repeat> 
    <apex:commandButton action="{!submit}" value="Submit"/>
    </apex:form>
</apex:page>

Apex class
 
public class Testctrl {

    List<StudentWrapper> sw {get; set;}
    public list<Attendence__c> attds = new list<Attendence__c>();
    
    class StudentWrapper {
       
       public Student__c sd {get; set;}
       public boolean selected {get; set;}
       
       public StudentWrapper(Student__c st) {
          
          sd = st;
          selected = false;         
       
       }
    
    }
    
    public List<StudentWrapper> getStudents() {
      
      if(sw == null) {
        
        sw = new List<StudentWrapper>();
      
        for(Student__c temp:[Select Id, Name from Student__c]) {
         
           StudentWrapper snew = new StudentWrapper(temp);
           sw.add(snew);
        
        }  
      }  
      
      return sw;
      
    }
    
    public void submit(){
        attds.clear();
        for(StudentWrapper sout:getStudents()){
             attds.add(new Attendence__c(Absent__c = sout.selected, Student__c = sout.sd.Id));
        }
        system.debug('************attendence*****************'+attds);
        insert attds;
    }
}

Kindly mark it as an answer if that works for you.
 
This was selected as the best answer
bob_buzzardbob_buzzard
The problem here is that you have a single input field that is used on every row, so the last one will "win" and its value will be used.

This is a case for our old friend, the wrapper class. There's an example of using this on my blog at:

http://bobbuzzard.blogspot.co.uk/2011/07/managing-list-of-new-records-in.html

It doesn't exactly match your use case, but should give you a grounding in the concept. So in your case you would have a wrapper class that would contain the Student__c record and the Boolean to capture the checkbox. You iterate a list of these in your apex:repeat, and when the user clicks save the wrapper class instance will contain the Student__c record and the user's selection for the checkbox, which you can use to create a child record.
Vijay Kumar Rebbala 11Vijay Kumar Rebbala 11
@Akhil Anil. Thanks for your reply. But i got an error while using your code. Error = "Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Date of Attendence]: [Date of Attendence]
Error is in expression '{!submit}' in component <apex:commandButton> in page testpage: Class.Testctrl.submit: line 44, column 1".

@bob_buzzard, Thanks for you reply. Looking into your blog.
Akhil AnilAkhil Anil

Hi Vijay,

You either need to mark "Date of Attendence" as not required in the data model OR provide a value to that field before inserting the "attds" records in the apex code.