You need to sign in to do that
Don't have an account?
Shiv 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 :
https://drive.google.com/file/d/0B-kKCGMuue-fdFpqT19YMDhWbEU/edit?usp=sharing
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">
<script>
function checkAll(cb)
{
var inputElem = document.getElementsByTagName("input");
for(var i=0; i<inputElem.length; i++)
{
if(inputElem[i].id.indexOf("checkedone")!=-1)
inputElem[i].checked = cb.checked;
}
}
</script>
<apex:form >
<apex:PageBlock title="Account">
<apex:pageBlockButtons >
<apex:commandButton value="Delete" action="{!deleteAll}"/>
</apex:pageBlockButtons>
<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>
</apex:facet>
<apex:inputCheckbox value="{!a.selected}" id="checkedone">
<apex:actionSupport event="onclick" action="{!GetSelected}" rerender="Selected_PBS"/>
</apex:inputCheckbox>
</apex:column>
<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:dataTable>
</apex:pageBlock>
</apex:form>
<apex:messages />
</apex:page>
*******************************************************************************************************************************************
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(){
selectedAccounts.clear();
for(accountwrapper accwrapper: accountList)
if(accwrapper.selected == true)
selectedAccounts.add(accwrapper.acc);
return null;
}
public List<Account> GetSelectedAccounts(){
if(selectedAccounts.size()>0)
return selectedAccounts;
else
return null;
}
public PageReference deleteAll(){
try{
List<Account> selAccounts=GetSelectedAccounts();
if(selAccounts.size()>0){
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.
Now in the controller side get the URL parameter "index" and may be you can do a remove :)
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 .
https://drive.google.com/file/d/0B-kKCGMuue-fSEpHVkJjNHNNa3c/edit?usp=sharing
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.
Thanks