Visualforce Page with Wrapper Class Custom Controller and Pagination > Apex and Visualforce
Visualforce Page with Wrapper Class Custom Controller and Pagination > Apex and Visualforce
Salesforce Development and Implementations
Wrapper Classes in Salesforce:
Lets start by understanding what a Wrapper Class is.
A wrapper or container class is a class, data structure, or an abstract data type whose instances are a collections of multiple objects. Its a custom type defined by the Salesforce Dev where he defines the properties of the wrapper class. Within Apex & Visualforce this can be extremely helpful to achieve many business scenarios within the Salesforce CRM software.
Using Wrapper classes we can have the ability to check few records from the displayed list and process them for some action.
Using Wrapper classes we can have the ability to check few records from the displayed list and process them for some action.
For example you may want to define a type that will contains some account object data along with some images and also some other custom object data. In order to accommodate this in a single type you cannot use any of the standard salesforce types [list / set / maps of a particular object type] you will have to define your own type and that will be wrapper class itself.
Here is a basic implementation of a wrapper class:
Wrappers with Pagination:
The tricky part is making wrapper classes work with pagination.
So here is the implementation where I have added wrapper class functionality to my earlier Custom Controller with Pagination example.
Code:
AccountWPController.apxc
public class AccountWPController {
public List<AccountWrapper> accsw = new List<AccountWrapper>();
public List<User> usrs;
public class AccountWrapper{
public Account acc{get;set;}
public Boolean isSelected{get;set;}
public AccountWrapper(Account a){
acc = a;
isSelected = false;
}
}
//limited display list
public List<AccountWrapper> accstoshow;
//Navigation Variables
Integer counter = 0; // To track number of records parsed
Integer limitSize = 20; // Number of records to be displayed
Integer totalSize = 0; // To store the total number of records available
//Our collection of the class/wrapper objects AccountWrapper
public List<AccountWrapper> AccountList {get; set;}
public AccountWPController(){
accstoshow = new List<AccountWrapper>();
List<Account> allaccs = [select ID, Name from Account ORDER BY Name LIMIT 999];
for(Account a : allaccs){
accsw.add(new AccountWrapper(a));
}
totalSize = accsw.size();
if((counter+limitSize) <= totalSize){
for(Integer i = 0; i<limitSize; i++)
accstoshow.add(accsw.get(i));
}
else{
for(Integer i=0;i<totalSize;i++)
accstoshow.add(accsw.get(i));
}
}
//Navigation methods
public void beginning(){
accstoshow.clear();
counter=0;
if((counter + limitSize) <= totalSize){
for(Integer i=0;i<limitSize;i++){
accstoshow.add(accsw.get(i));
}
} else{
for(Integer i=0;i<totalSize;i++){
accstoshow.add(accsw.get(i));
}
}
}
public void next(){
accstoshow.clear();
counter=counter+limitSize;
if((counter+limitSize) <= totalSize){
for(Integer i=counter-1;i<(counter+limitSize);i++){
accstoshow.add(accsw.get(i));
}
} else{
for(Integer i=counter;i<totalSize;i++){
accstoshow.add(accsw.get(i));
}
}
}
public void previous(){
accstoshow.clear();
counter=counter-limitSize;
for(Integer i=counter;i<(counter+limitSize); i++){
accstoshow.add(accsw.get(i));
}
}
public void last (){
accstoshow.clear();
if(math.mod(totalSize , limitSize) == 0){
counter = limitSize * ((totalSize/limitSize)-1);
} else if (math.mod(totalSize , limitSize) != 0){
counter = limitSize * ((totalSize/limitSize));
}
for(Integer i=counter-1;i<totalSize-1;i++){
accstoshow.add(accsw.get(i));
}
}
public Boolean getDisableNext(){
if((counter + limitSize) >= totalSize )
return true ;
else
return false ;
}
public Boolean getDisablePrevious(){
if(counter == 0)
return true ;
else
return false ;
}
public List <AccountWrapper> getAccs(){
return accstoshow;
}
public List <User> getUsrs(){
return usrs;
}
public pageReference updateUsrs(){
usrs = [select Id, Username, Firstname, Lastname, Name, Email, UserType from User];
return null;
}
public PageReference processSelected() {
System.debug('entering processing ..');
//We create a new list of Accounts that wil be populated only with Account if they are selected
List<Account> selectedAccounts = new List<Account>();
//We will cycle through our list of AccountWrapper and will check to see if the selected property is set to true, if it is we add the Account to the selectedAccounts list
for(AccountWrapper ac: getAccounts()) {
if(ac.isSelected == true) {
System.debug('process going on');
selectedAccounts.add(ac.acc);
}
}
// Now we have our list of selected account and can perform any type of logic we want, sending emails, updating a field on the Account, etc
System.debug('Results');
for(Account ac: selectedAccounts) {
System.debug(ac);
}
AccountList=null; // we need this line if we performed a write operation because getAccounts gets a fresh list now
return null;
}
//This method uses a simple SOQL query to return a List of Account
public List<AccountWrapper> getAccounts() {
if(AccountList == null) {
AccountList = accsw;
}
return AccountList;
}
}
AccountWPList.vfp
<apex:page controller="AccountWPController" sidebar="false" showHeader="false" lightningStylesheets="true">
<apex:form >
<apex:pageBlock title="List of Accounts and Users from Custom Controller">
<apex:pageBlockSection title="Current User Info">
<br />
<h1>
Current User : {! $User.FirstName } {! $User.LastName }
( {! $User.Username } )
</h1>
<br />
<p> Today's Date is {! TODAY() } </p>
<p> So Next week it will be {! TODAY() + 7 } </p>
<p>{! IF( CONTAINS('salesforce.com','force.com'),
'Yep', 'Nope') }</p>
<p>{! IF( DAY(TODAY()) < 15,
'Before the 15th', 'The 15th or after') }</p>
<br /><br />
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton value="<<" action="{!beginning}" disabled="{!DisablePrevious}"/>
<apex:commandButton value="<" action="{!previous}" disabled="{!DisablePrevious}"/>
<apex:commandButton value=">" action="{!next}" disabled="{!DisableNext}"/>
<apex:commandButton value=">>" action="{!last}" disabled="{!DisableNext}"/>
<apex:commandButton action="{!updateUsrs}" value="Users" title="Show Users"/>
<apex:commandButton action="{!processSelected}" value="Process Selected"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Accounts and Users" columns="2">
<apex:pageBlockTable title="Account Info" value="{!Accs}" var="a">
<apex:column >
<apex:inputCheckbox value="{!a.isSelected}"/>
</apex:column>
<apex:column value="{!a.acc.Name}"/>
</apex:pageBlockTable>
<apex:pageBlockTable title="User Info" value="{!Usrs}" var="u">
<apex:column value="{!u.Name}"/>
<apex:column value="{!u.Email}"/>
<apex:column value="{!u.UserType}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Comments
Post a Comment