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

 

In the controller code section, paste the following:

public class wrapperClass {

public String accinp { get; set; }

public listaccs = new list();

public listaccwrap{get; set;}

public listselectedaccount{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();

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();

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