Wrapper Class is a class within a class that stores a group of different or similar data type/Sobject values into a single object.
In other words, it is a collection of data members only without methods or user-defined data types.
In other words, it is a collection of data members only without methods or user-defined data types.
We can say like "It’s a data structure or an abstract data type that can contain different objects or collections of objects as its members." and its member could be 'Primitive Data type' 'Collection Data type' or 'sobjects'
Benefits Of Wrapper Class in Salesforce:
- With the use of Wrapper class, we can store the data from multiple sObject.
- With the use of Wrapper class, we don’t require multiple lists of different sObject.
- Code re-usability.
- We can use wrapper class in Visualforce Page, Lightning Component and we can also convert the JSON string into wrapper class.
- We can use multiple wrapper classes in a single Apex Class.
- We can use a nested wrapper class in a single Apex Class.
Apex Syntax:-
public class WrapperClass {
public static AccountWrapperClass demoWrapperClassMethod(String AccountNamestringValue){
List <Account> accList = [Select id, name, (Select id, name from contacts),(Select id, name From opportunities), (Select id from cases) from Account Where name =: AccountNamestringValue];
AccountWrapperClass accwrapper = new AccountWrapperClass();
accwrapper.conList = accList[0].contacts;
accwrapper.oppList = accList[0].Opportunities;
accwrapper.caseList = accList[0].Cases;
System.debug(accwrapper);
return accwrapper;
}
//Wrapper class
public class AccountWrapperClass{
List <contact> conList;
List <opportunity> oppList;
List <case> caseList;
}
}
Example :-
1 ) :- Wrapper Class Example Using Visualforce Salesforce
Feature :-
