Wrapper Class In Salesforce -Explained with Live Example
Before explaining what is wrapper class , lets discuss what is a Custom Object in Salesforce ?
Simplest Definition of a Custom Object would be - "Custom Object is a collection of Custom Fields which can be of Different Data Types".
Similarly you can assume a Wrapper Class as a collection of Several Data Types.
Now lets take an example to understand Wrapper Class. One of the most commonly asked questions , "How can I display a table of records with a check box and then process only the records that are selected?
Lets Say the Table to be displayed is Account table. Now If we clearly Observe the question asked , it requires two things in a list to be displayed , Can you guess them ?
1) Record of Account Table
2) Checkbox to select that record.
As we already mentioned that a wrapper class is a collection of Data types, lets go ahead and create a Wrapper class with variable to hold Account record and another to hold Boolean value for a check box.
Copy and Paste the below Codes:
Apex Controller
public class WOS_WrapperExample { public List<accountwrap> accountList{get;set;} public List<Account> selectedAccounts {get;set;} public WOS_WrapperExample() { accountList = new list<accountwrap>(); selectedAccounts= new list<account>(); for(Account a : [select Id, Name, AccountNumber, Phone from Account limit 10]) accountList.add(new accountwrap(a)); // Generating the list of Wrapper } public PageReference Selectedacc() { selectedAccounts.clear(); for(accountwrap accwrapper : accountList) if(accwrapper.selected == true) selectedAccounts.add(accwrapper.acc); return null; } public class accountwrap { // Wrapper Class with two variables public Account acc{get; set;} public Boolean selected {get; set;} public accountwrap(Account a) // Constructor of wrapper class { acc = a; selected = false; } } }
Visualforce Page :
<apex:page Tabstyle="Account" controller="WOS_WrapperExample"> <apex:form > <div style="float:left;margin: 10px;"> <h1>Account list</h1> <table> <tr> <th>Select</th> <th>Account Name</th> <th>Phone</th> </tr> <apex:repeat value="{!accountList}" var="a"> <tr> <td><apex:inputCheckbox value="{!a.selected}" id="checkedone" /></td> <td>{!a.acc.Name}</td> <td>{!a.acc.phone}</td> </tr> </apex:repeat> </table> </div> <div style="float:left;margin: 10px;"> <h1>For Selected Account List Click <apex:commandButton value="Get Selected" action="{!Selectedacc}" reRender="selAcc"/></h1> <apex:outputPanel id="selAcc"> <table> <tr> <th>Account Name</th> <th>Phone</th> </tr> <apex:repeat value="{!selectedAccounts}" var="a"> <tr> <td>{!a.Name}</td> <td>{!a.phone}</td> </tr> </apex:repeat> </table> </apex:outputPanel> </div> </apex:form> </apex:page>
Sir,
ReplyDeleteCan We solve above scenario by using standardsetcontroller?
This is a common example found in any website.. Can you list few more scenarios where Wrapper classes can be of use. Code for the same might not be needed but atleast few more scenarios.
ReplyDeleteInteresting post! This is really helpful for me. I like it! Thanks for sharing!
ReplyDeleteWebseiten Gestaltung Lüdenscheid
I found a lot of interesting information here. A really good post
ReplyDeleteoffice 2011 mac download deutsch
ReplyDeletereally good
jobbörse südwestfalen
really good
ReplyDeleteJobvermittlung
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
ReplyDeleteBest Spring Classroom Training Institute
Best Devops Classroom Training Institute
Best Corejava Classroom Training Institute
Best Oracle Classroom Training Institute
Best Sql Server Classroom Training Institute
Really nice post. Provided a helpful information.I hope that you will post more updates like this
ReplyDeleteAWS Online Training
This is an awesome post.Really very informative and creative contents.
ReplyDeleteSEO company in Chennai
Thanks for sharing valuable information.
ReplyDeleteDigital Marketing training Course in Chennai
digital marketing training institute in Chennai
digital marketing training in Chennai
digital marketing course in Chennai
digital marketing course training in omr
digital marketing certification in omr
digital marketing course training in velachery
digital marketing training center in Chennai
digital marketing courses with placement in Chennai
digital marketing certification in Chennai
digital marketing institute in Chennai
digital marketing certification course in Chennai
digital marketing course training in Chennai
Digital Marketing course in Chennai with placement
digital marketing courses in Chennai