Share Your Experience With Others

What is a Wrapper Class in Salesforce?Why we need Wrapper class?

Wrapper Class in Apex Salesforce : A wrapper or container class is a class, a data structure, or an abstract data type which contains different objects or collection of objects as its members.

A wrapper class is a custom object defined by programmer wherein he defines the wrapper class properties.

Consider a custom object in salesforce, what do you have in it? fields right? different fields of different data types.

Similarly wrapper class is a custom class which has different data types or properties as per requirement.

We can wrap different objects types or any other types in a wrapper class.

In visualforce, we can operate on one object at a time and can display data related to that particular object using page block table.
What if we want data from a number of objects?
In this case, we can make use of the wrapper class.
Let say if I have two objects and these objects do not have any relationship in between them and I want to display data from both these objects in the table.

Object 1: Account

Fields of Account : id, name

Object 2: Suggestion

Fields of Suggestion : id, name

Apex Controller:

Wrapperoftwoobjects

Visualforce Page:

vfpageoftwowrapperobject

Output:

outputoftwoobjectswrapper

Another most important use is when you want to display data with check-boxes and then display selected data based on a checkbox.

Apex Controller:

simplewrapperclass.PNG

Visualforce page:

simplewrappervfpage

Output:

outputofsimplewrapper

Leave a comment