What is Wrapper Class in Salesforce Apex?
Wrapper Class in Salesforce Apex A wrapper or container class is a class, a data structure, or an abstract data type that contains different objects or collections of objects as its members.
A wrapper class is a custom object described by a programmer in which he defines the wrapper class properties. Consider a custom object in salesforce, what do you have in it? fields right? different fields of various data types. Similarly wrapper class is a custom class that has different data types or properties as per requirement. We can wrap different object types or another types in a wrapper class.
do you know how to count child records on parents? trigger to count the number of related child records on the parent.
Below example, we have created our own list with two different data types.
public class wrapper {
public void callwrapper(){
List<wrapperclass> lst=new List<wrapperclass>();
contact cnn=[select id,firstname from contact limit 1];
for(Account acc_new:[select id,name from Account]){
wrapperclass obj=new wrapperclass(acc_new);
lst.add(obj);
}
wrapperclass obj1=new wrapperclass(cnn);
lst.add(obj1);
system.debug(lst.size());
}
public class wrapperclass{
public string name;
public wrapperclass(Account x1){
name=x1.name;
}
public wrapperclass(contact x1){
name=x1.firstname;
}
}
}
For any Query/doubt comment below and for quick response LinkedIn and Twitter.