You need to sign in to do that
Don't have an account?
Shruthi GM 4
Need to display all the accounts along with checkbox on vf page.
Hi all,
I have a requirement to display all the accounts present on a vf page along with the checkboxes beside them so that user can select any no of accounts on the page.What is the code for this?
Please help.
Thanks inadvance.
I have a requirement to display all the accounts present on a vf page along with the checkboxes beside them so that user can select any no of accounts on the page.What is the code for this?
Please help.
Thanks inadvance.
1) http://www.sfdcpoint.com/salesforce/wrapper-class-in-apex/
2) https://developer.salesforce.com/page/Wrapper_Class
3) https://www.minddigital.com/what-is-wrapper-class-and-how-to-use-it-in-salesforce/
Please let us know if this will help you
Thanks
Amit Chaudhary
All Answers
Add this this Java script code also at the end, it will check/uncheck all check boxes on select of header checkbox.
You can use wrapper class to display accounts on you vf page with checkbox to select. Look at the post to know more about wrapper classes.
https://developer.salesforce.com/page/Wrapper_Class
Add this wrapper class in controller(u need to do some research how to use this, which u will get easily in google)
Here we need to use Standard controller Account? or is it the custom controller?
1) http://www.sfdcpoint.com/salesforce/wrapper-class-in-apex/
2) https://developer.salesforce.com/page/Wrapper_Class
3) https://www.minddigital.com/what-is-wrapper-class-and-how-to-use-it-in-salesforce/
Please let us know if this will help you
Thanks
Amit Chaudhary
Please help.
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000DBoRIAW
Please check Working on below link :-
http://amitblog-developer-edition.ap1.force.com/apex/CustomPaginationDemo
That example i posted for COntact object. I hope you can convert that to Accoun
* Description: This is the controller for the Visualforce page :- HTMLCheckboxDemoPage
* It handles the processing of multiple opportunities selected using checkboxes on the VF page and updates the
* Area__c custom field of these opportunities. It also enables pagination to handle the case of more than 1000
* cannot be displayed at once in <apex:pageBlockTable> tag in the VF page.
*/
global class HTMLCheckboxDemoCtlr
{
public Id oppID {get;set;}
private List<Opportunity> oppList;
public List<Opportunity> oppRecords{get;set;}
public ApexPages.StandardSetController setCon{get;set;}
public Integer currentPageNumber {get;set;}
public Integer highestPageNumber {get;set;}
public Integer recordCount {get;set;}
public Integer intPageSize;
public HTMLCheckboxDemoCtlr()
{
intPageSize = 4;
recordCount = 0;
highestPageNumber = 0;
currentPageNumber = 0;
oppID = ApexPages.currentPage().getParameters().get('Id');
oppList = [Select Id, Name, Area__c from Opportunity order by Name limit 50000];
recordCount = oppList.size();
highestPageNumber = calculateHighestPageNumber(recordCount);
if(recordCount > 0)
{
currentPageNumber = 1;
}
oppRecords = new List<Opportunity>();
setCon = new ApexPages.StandardSetController(oppList);
setCon.setPageSize(4);
oppRecords = (List<Opportunity>)setCon.getRecords();
}
public pageReference processSelected()
{
List<Opportunity> selectedOpp = new List<Opportunity>();
Integer counterCurrentPageNumber = 1;
Map<Id, Opportunity> mapOppShown = new Map<Id, Opportunity>();
for(Opportunity opp : oppRecords)
{
mapOppShown.put(opp.Id, opp);
}
String para = Apexpages.currentPage().getParameters().get('node');
List<id> updateTheseOpp = para.split(',');
for(Id oppId : updateTheseOpp)
{
if(mapOppShown.containsKey(oppId))
{
selectedOpp.add(mapOppShown.get(oppId));
}
}
if(selectedOpp.size() > 0)
{
for(Opportunity oppObj : selectedOpp)
{
oppObj.Area__c = 'processed';
}
update selectedOpp;
// For avoiding error :- Modified rows exist in the records collection :- Query records again
oppList = [Select Id, Name, Area__c from Opportunity order by Name limit 50000];
recordCount = oppList.size();
highestPageNumber = calculateHighestPageNumber(recordCount);
oppRecords = new List<Opportunity>();
setCon = new ApexPages.StandardSetController(oppList);
setCon.setPageSize(4);
oppRecords = (List<Opportunity>)setCon.getRecords();
// Got to navigate till paginationCurrentPageNumber to display same page again, it could be the 2nd or 3rd page,
// on which "Update Area" was clicked after selecting opportunities using checkboxes.
while(currentPageNumber > counterCurrentPageNumber)
{
moveNext();
counterCurrentPageNumber = counterCurrentPageNumber + 1;
}
if(selectedOpp.size() > 1)
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'Updated ' + selectedOpp.size() + ' opportunities'));
}
else
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'Updated ' + selectedOpp.size() + ' opportunity'));
}
}
return null;
}
public void moveNext()
{
if(setCon.getHasNext())
{
oppRecords = new List<Opportunity>();
setCon.next();
if(currentPageNumber < highestPageNumber)
{
currentPageNumber = currentPageNumber + 1;
}
oppRecords = (List<Opportunity>)setCon.getRecords();
}
}
public void movePrevious()
{
if(setCon.getHasPrevious())
{
oppRecords = new List<Opportunity>();
setCon.previous();
if(currentPageNumber > 1)
{
currentPageNumber = currentPageNumber - 1;
}
oppRecords = (List<Opportunity>)setCon.getRecords();
}
}
public void moveFirst()
{
oppRecords = new List<Opportunity>();
setCon.first();
currentPageNumber = 1;
oppRecords = (List<Opportunity>)setCon.getRecords();
}
public void moveLast()
{
oppRecords = new List<Opportunity>();
setCon.last();
currentPageNumber = highestPageNumber;
oppRecords = (List<Opportunity>)setCon.getRecords();
}
public Integer calculateHighestPageNumber(Integer recordCount)
{
if(math.mod(recordCount,intPageSize) != 0)
{
Integer modResult = recordCount/intPageSize;
return (modResult + 1);
}
else
{
return recordCount/intPageSize;
}
}
}
<!--
VF page :- HTMLCheckboxDemoPage
Browse for this page using URL :- https://c.ap5.visual.force.com/apex/HTMLCheckboxDemoPage?id=0067F00000IOlJj
where 0067F00000IOlJj is the ID of any opportunity record.
-->
<apex:page controller="HTMLCheckboxDemoCtlr" sidebar="false" showHeader="false" cache="false">
<apex:form>
<apex:pageMessages id="ResultMessages"/>
<script>
var flag=0;
var SelectConId1='';
var flag2 = 0;
var countchbox=0;
function checkAll(cb)
{
flag=0;
// List of IDs of the opportunities
SelectConId1='';
var inputElem = document.getElementsByTagName("input");
for(var i=1; i<inputElem.length; i++)
{
if(inputElem[i].id.indexOf("checkedone")!=-1)
{
inputElem[i].checked = cb.checked;
flag=flag+1;
SelectConId1=SelectConId1+inputElem[i].name+',';
}
}
if(cb.checked!=true)
{
SelectConId1="";
flag=0;
}
}
// This function will get called when any 1 checkbox is checked
function checkone(cb)
{
countchbox=0;
if(cb.checked)
{
flag=flag+1;
if((SelectConId1.length)<=1)
{
SelectConId1=cb.name+',';
}
else
{
SelectConId1=SelectConId1+cb.name+',';
}
}
else if((cb.checked)!=true)
{
flag=flag-1;
if((SelectConId1.length)<=1)
{
SelectConId1=cb.name+',';
}
else
{
var allids=SelectConId1.split(',');
var idarray='';
for(var i=0;i<allids.length;i++)
{
if(allids[i]!=cb.name && allids[i]!='')
{
//alert('this id wont be removed');
idarray=idarray+allids[i]+',';
}
else
{
if(allids[i] == cb.name)
{
//alert('Removing this id as match found');
}
if(allids[i] == '')
{
//alert('removing blank');
}
}
}
SelectConId1=idarray;
}
}
var inputElem = document.getElementsByTagName("input");
for(var i=0; i<inputElem.length; i++)
{
if(inputElem[i].id.indexOf("checkedone")!=-1)
{
countchbox=countchbox+1;
}
}
if(flag==countchbox)
{
document.getElementById("main").checked=true;
//alert('last checkbox is checked, so checkAll auto check doing');
}
else
{
document.getElementById("main").checked=false;
}
}
function updateArea()
{
if((SelectConId1.length)<=1)
{
alert('Select opp');
}
else
{
flag2 = flag;
flag = 0;
if(flag2 == 1)
{
alert(flag2 + ' opportunity will get updated');
}
else
{
alert(flag2 + ' opportunities will get updated');
}
paraFunction(SelectConId1);
}
SelectConId1 = '';
}
</script>
<apex:outputPanel>
Total number of opportunities: <apex:outputLabel value="{!recordCount}"/>
</apex:outputPanel>
<br/>
<apex:outputPanel id="EnclosingPanel">
<apex:outputPanel id="PanelOpp" rendered="{!oppRecords.size>0}">
<apex:pageBlock>
<apex:pageBlockButtons>
<apex:commandButton value="Update Area" onclick="updateArea()"/>
</apex:pageBlockButtons>
<apex:outputPanel>
Select the checkboxes below for the opportunites whose Area you would like to update
</apex:outputPanel>
<apex:pageBlockTable id="outputTable" value="{!oppRecords}" var="cOpp">
<apex:column >
<apex:facet name="header">
<input type="checkbox" id="main" onclick="return checkAll(this)"/>
</apex:facet>
<input type="checkbox" name="{!cOpp.id}" id="checkedone" onclick="return checkone(this)"/>
</apex:column>
<apex:column title="Opportunity ID" value="{!cOpp.Id}"/>
<apex:column title="Opportunity Name" value="{!cOpp.Name}"/>
<apex:column title="Area" value="{!cOpp.Area__c}"/>
</apex:pageBlockTable>
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:outputpanel >
<apex:commandButton action="{!moveFirst}" value="First" disabled="{!!setCon.hasPrevious}"/>
<apex:commandButton action="{!movePrevious}" value="Previous" disabled="{!!setCon.hasPrevious}"/>
<apex:commandButton action="{!moveNext}" value="Next" disabled="{!!setCon.hasNext}"/>
<apex:commandButton action="{!moveLast}" value="Last" disabled="{!!setCon.hasNext}"/>
<br/>
Page no <apex:outputText value="{!currentPageNumber}"/>
</apex:outputpanel>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>
</apex:outputPanel>
<apex:actionFunction name="paraFunction" action="{!processSelected}" reRender="EnclosingPanel,ResultMessages">
<apex:param id="anode" name="node" value=""/>
</apex:actionFunction>
</apex:form>
</apex:page>
https://www.theunitedsolutions.in/what-is-wrapper-class-in-salesforce/