+ Start a Discussion

How to Update selected records using inputCheckbox on a visualforce page



I have created a visualforce page where i am displaying all the records together to update simultaneously,


But Instead of  updating all the records in a grid, I need to update only the selected fields by using the checkbox..

Please suggest the possible solution to do this task..


Thank you!!

Ritesh AswaneyRitesh Aswaney

Iterate through the collection of records in your controller, and add the records with the checkbox checked into another collection.

Then process the collection created as a result of above.


can you post sample code that determine if checkbox  is equal to true.. thanks


This is my Attendance update page, And currently it updates all the records together.

But my task is to update only the selected values through the checkbox, and i just created a UI checkbox only to select

and deselect.. Is it posssible to do update with the UI check boxes????


Here my code is, just have a look and give your suggestion,


This is what i used as a controller:


public class Checkbox_Class
public PageReference save()
update a;
return null;

public PageReference cancel()
return null;

List<accountwrapper> accountList = new List<accountwrapper>();
List<Attendance__c> selectedAccounts = new List<Attendance__c>();
List<Attendance__c> a = new List<Attendance__c>();

public List<accountwrapper> getAccounts()
for(Attendance__c a : [SELECT Name, date__c, Excused_Absence__c, Excuse_Details__c,
Present__c, Rubric__c,Session__c, Youth__r.LastName FROM Attendance__c
ORDER BY Youth__r.LastName asc limit 5])

accountList.add(new accountwrapper(a));
return accountList;

public PageReference getSelected()
for(accountwrapper accwrapper : accountList)
if(accwrapper.selected == true)
return null;

public List<Attendance__c> GetSelectedAccounts()
return selectedAccounts;
return null;


public class accountwrapper
public Attendance__c acc{get; set;}
public Boolean selected {get; set;}
public accountwrapper(Attendance__c a)
acc = a;
selected = false;


And the visualforce code is:


<apex:page controller="Checkbox_Class" Tabstyle="Contact" sidebar="false" showHeader="false">
<apex:form >

<apex:pageBlock Title="Selected value with CheckBoxes">

<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}" OnClick="show_alert()" />
<apex:commandButton value="Cancel" action="{!cancel}" onclick="confirmcancel()"/>

<apex:pageBlockTable value="{!accounts}" var="a" >
<apex:column >
<apex:facet name="header">
<apex:inputCheckbox >
<apex:actionSupport event="onclick" action="{!GetSelected}" onsubmit="checkAll(this)" rerender="Selected_PBS"/>

<apex:inputCheckbox value="{!a.selected}" id="checkedone">
<apex:actionSupport event="onclick" action="{!GetSelected}" rerender="Selected_PBS"/>

<apex:column headervalue="Name" value="{!a.acc.Name}" />
<apex:column headervalue="Date" value="{!a.acc.date__c}" />

<apex:column headervalue="Excused Absence">
<apex:inputfield value="{!a.acc.Excused_Absence__c}" />

<apex:column headervalue="Present">
<apex:inputfield value="{!a.acc.Present__c}" />

<apex:column headervalue="Excuse Details">
<apex:inputField value="{!a.acc.Excuse_Details__c}" />

<apex:column headervalue="Rubric(#)">
<apex:inputfield value="{!a.acc.Rubric__c}" />

<apex:column headervalue="Session" value="{!a.acc.Session__c}" />
<apex:column headervalue="Youth Last Name" value="{!a.acc.Youth__r.LastName}" />


function checkAll(cb)
var inputElem = document.getElementsByTagName("input");
for(var i=0; i<inputElem.length; i++)
inputElem[i].checked = cb.checked;