What Is Wrapper Class & How To Use It In Salesforce

What Is Wrapper Class & How To Use It In Salesforce

By admin November 28, 2013
Blog, Salesforce Development 2

A wrapper or container class is a class, data structure, or an abstract data type whose instances are a collections of other objects.It is a custom object defined by Salesforce developer 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.

Use of Wrapper Class within Salesforce:

Many times in the community boards, people have asked the questions on how they can display a table of records with a check box and then process only the records that are selected. You can do it using this below step by step instructions. You can use wrapper class to display records from multiple records within a single table based around the business needs.

Here is an example if you want to display account data in the table along with a checkbox on the right side of the table (checkbox with every row). You can bind an account checkbox field to this right column but below is an example for demonstrating wrapper with a custom checkbox column.
Follow these simple steps within your Salesforce CRM:

Step 1 –

Go to setup→Develop→Pages

Click the “New” Button for creating new pages:

Develop page

Step 2-

In the label and name box, type “WrapperClass”

Develop page

Step 3 –

In editor paste this code:

In the Visualforce page code section, paste the following:

Develop page

<apex:page controller=”wrapperClass” sidebar=”false” showHeader=”false”>

<apex:form>

<apex:pageMessages />

<apex:pageBlock title=”Edit Information in Account” mode=”edit”>

<apex:pageBlockSection>

<apex:pageBlockSectionItem>

<apex:commandButton value=”Change Type” action=”{!Changetype}” reRender=”theTable”/>

<apex:inputField value=”{!anacc.type}”/>

</apex:pageBlockSectionItem>

</apex:pageBlockSection>

<apex:pageBlockTable value=”{!accwrap}” var=”a” id=”theTable”>

<apex:columnheaderValue=”All accounts name”>

<apex:inputCheckbox value=”{!a.selected}”/>

<apex:actionSupport event=”onclick” action=”{!getselected}” reRender=”other_block”/>&nbsp;

<apex:outputField value=”{!a.acc.name}”/>

</apex:column>

<apex:columnheaderValue=”Owner name”>

<apex:outputField value=”{!a.acc.Ownerid}”/>

</apex:column>

<apex:columnheaderValue=”Type”>

<apex:outputField value=”{!a.acc.Type}”/>

</apex:column>

<apex:columnheaderValue=”Last modify “>

<apex:outputField value=”{!a.acc.Website}”/>

</apex:column>

<apex:columnheaderValue=”Last modify “>

<apex:outputField value=”{!a.acc.Price__c}”/>

</apex:column>

</apex:pageBlockTable>

</apex:pageBlock>

</apex:form>

</apex:page>

In the controller code section, paste the following:

public class wrapperClass {

public String accinp { get; set; }

public list<account>accs = new list<account>();

public list<accountwrapper>accwrap{get; set;}

public list<account>selectedaccount{get; set;}

public account anacc{get; set;}

public void Changetype(){

for(account ac:selectedaccount){

ac.type=anacc.type;

}

if(anacc.Price__c<400){

update selectedaccount;

}else{

apexpages.addmessage(new apexpages.message(apexpages.severity.error,’This is only applicable for Price less than 400…..’));

}

}

publicwrapperClass(){

accwrap=new list<accountwrapper>();

anacc= new account();

accs= [select name, Ownerid, Website, Type,Price__c from account where website!= null order by createddatedesc limit 100];

accountWrapperObject();

}

public void accountWrapperObject(){

for(account a:accs){

accountwrapper aw = new accountwrapper(a);

accwrap.add(aw);

}

}

public void getselected(){

selectedaccount = new list<account>();

selectedaccount.clear();

for(accountwrapperawrap:accwrap)

if(awrap.selected==true){

selectedaccount.add(awrap.acc);

}

}

public class accountwrapper{

public account acc{get; set;}

public boolean selected{get; set;}

public accountWrapper(account a){

acc=a;

selected= false;

}

}

}

“Save” this code

Step 4 –

Open page on Preview panelbar:

Step 5 –

Please check that it is showing within your browser and you can make changes to it as mentioned above.If you have any questions or need any technical help, please feel free to reach out to us and chat to our expert Salesforce developers who would be more than happy to help. We will keep on posting different questions we get from users regarding any Salesforce development help they need. Keep on checking back here for more interesting Salesforce development & customization help!

Related Posts

Popular post