+ Start a Discussion
Shiv ShankarShiv Shankar 

What is the best way to send selected recordIds from the VisualForce Page to the Controller

Hi All,


I have one scinario. in that scinario I am making a multiple record editing page. Where user can add, Edit , delete multiple records at a time.

I have some of the fields required in that object. Please have a look of following image for better understading : 




I want to know what is the best way to send selected recordIds from the VF Page to the Controller.



Hi Shiv,

Have you considered using wrapper class in this scenario?

Mass add/delete can be done implemented easily using wrapper classes in apex.


The following code snippet might be helpful for you 


<apex:page controller="BulkDeleteController" tabStyle="Account">


function checkAll(cb)


var inputElem = document.getElementsByTagName("input");

for(var i=0; i<inputElem.length; i++)



inputElem[i].checked = cb.checked;




<apex:form >

<apex:PageBlock title="Account">

<apex:pageBlockButtons >

<apex:commandButton value="Delete" action="{!deleteAll}"/>


<apex:dataTable value="{!accounts}" var="a" columnswidth="50px,50px" cellpadding="4" border="1">

<apex:column >

<apex:facet name="header">

<apex:inputCheckbox >

<apex:actionSupport event="onclick" action="{!GetSelected}" onsubmit="checkAll(this)"/>



<apex:inputCheckbox value="{!a.selected}" id="checkedone">

<apex:actionSupport event="onclick" action="{!GetSelected}" rerender="Selected_PBS"/>



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

<apex:column headervalue="Account Number" value="{!a.acc.AccountNumber}" />

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




<apex:messages />



public class BulkDeleteController


public List<accountwrapper> accountList {get;set;}

public List<Account> selectedAccounts = new List<Account>();

public List<accountwrapper> getAccounts(){

//accountList = new List<accountwrapper>();

// for(Account a: [select Id, Name, AccountNumber, Phone from Account limit 5])

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

return accountList;


public BulkDeleteController (){

accountList = new List<accountwrapper>();

for(Account a: [select Id, Name, AccountNumber, Phone from Account limit 5])

accountList.add(new accountwrapper(a));

//return accountList;


public PageReference getSelected(){


for(accountwrapper accwrapper: accountList)

if(accwrapper.selected == true)


return null;



public List<Account> GetSelectedAccounts(){


return selectedAccounts;


return null;


public PageReference deleteAll(){


List<Account> selAccounts=GetSelectedAccounts();


for(Account ac:selAccounts){

delete ac;

System.debug('###########Data Deleted Successfully###########');



ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR, 'Deleted successfully'));

PageReference pageref=new PageReference('/apex/BulkDelete');

return pageref;


Catch(Exception e){

ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR, 'Data Base Error'));

return null;



public class accountwrapper {

public Account acc{get; set;}

public Boolean selected {get; set;}

public accountwrapper(Account a){

acc = a;

selected = false;






Since you are using a "Add" feature I belive the records wont be IN DB at all and hence they dont have a Id. So to delete those records from the LIST you have to build a indexing system. By default LIST index starts from 0 and so ON. You have to replicate the same in VF.


So I guess the best cadidate will be using APEX:Variable to generate a INDEXING @ VF.


<apex:variable value="{!0}" var="index"/>
<apex:pageBlockTable value="myList" var="ml" id="pb">
       <apex:commandbutton action="{!Del}" rerender="pb" immediate="true" value="Delete">
          <apex:param value="{!index}" name="index"/>
       <apex:variable var="index" value="{!index+1}"/>

   <apex:column value="{!myList.Name}"/>

 Now in the controller side get the URL parameter "index" and may be you can do a remove :)


public class myController{
   public List<Account> myList{get;set;}
   public void del(){
     Integer index = (integer)Apexpage.currentPage().getParameters().get('index');


Shiv ShankarShiv Shankar

Hi Avi,


Thanks for your valuable reply. But i have some additional scinario , We have multiple editing functionality also. In below image you can see above 2 records have Id's also. but 3rd record is not having .




What i want to know :


Suppose we have no of records which all are having record id than which approach should we use. Code which you have shared in that we are adding 'Del' button with every record. Acttuly i willing to know how can we pass a ids / index of selected records to the controller. I am using a single delete button for this.


I am wating for your valuable reply once again.