function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Teach_me_howTeach_me_how 

What is wrapper class?

Hi everyone. newbie here does anyone can explain me what is wrapper class particularly in apex programming what is the usage of this . thanks

Teach_me_howTeach_me_how

when should i use wrapper class?

Ankit AroraAnkit Arora

Here is the complete explanation with example. Hope it will clear all your queries.

 

http://wiki.developerforce.com/page/Wrapper_Class

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

vivek kasiviswanathanvivek kasiviswanathan
Wrapper Class Example :)
Wrapperclass to join two unrelated custom objects.
obj1 - Student (Id, Name)
obj2 - Subject (Id & amount)

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class wrapperClassController{

public List<cStudentDetail> stsubject {get; set;}

//constructor that populates the wrapper class with data and
//the updates the list that is exposed to the VF page
public wrapperClassController(){   

    stsubject = new List<cStudentDetail>();
    for (Student__c sd: [select id__c, name__c from Student__c]){
        for (Subject__c sb: [select id__c,amont__c from Subject__c]){
        cStudentDetail stsud = new cStudentDetail();
        stsud.StudentId = sd.id__c;
        stsud.StudentName = sd.name__c; 
        stsud.SubjectId = sb.id__c;
        stsud.SubjectAmount = sb.amont__c;  
        stsubject.add(stsud);
        }            
     } 
}

//wrapper class has custom fields to hold values from both the objects
public class cStudentDetail{
public string StudentId {get; set;}
public string StudentName {get; set;}
public string SubjectId {get; set;}
public string SubjectAmount {get; set;}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<apex:page controller="wrapperClassController">
<apex:pageBlock >
<apex:dataTable value="{!stsubject}" var="s" cellPadding="4" border="1">
<apex:column >
        <apex:facet name="header">StudentID</apex:facet>
        {!s.StudentId}
</apex:column>
<apex:column >
        <apex:facet name="header">StudentName</apex:facet>
        {!s.StudentName}
</apex:column>
<apex:column >
        <apex:facet name="header">SubjectID</apex:facet>
        {!s.SubjectId}
</apex:column>
<apex:column >
        <apex:facet name="header">SubjectAmount</apex:facet>
        {!s.SubjectAmount}
</apex:column>
</apex:dataTable>
</apex:pageBlock>
</apex:page>
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
User-added image