Wrapper Class in Java

Wrapper Class in Java | The Salesforce Developer Guide

A Wrapper class whose instances are collections of other objects. It is utilized to show Unique objects on a VisualForce page in the same table. The wrapper class in Java provides the mechanism to convert primitive into object and object into primitive. Exactly the same in Salesforce development.

Consider a custom object in Salesforce, what do you've got in it? fields right? distinctive fields of distinctive data types. So also the wrapper class could be a custom class that has diverse data types or properties as per necessity. We are going to wrap different objects sorts or any other sorts in a wrapper class. To wrap distinctive data objects, there's nothing superior to a wrapper class within the Salesforce. A wrapper course is also a custom class that has distinctive properties and information sorts, which can be decided to agree to the prerequisite.

Here is the Key Point of Wrapper Class

  • There is no need to browse the components or pass a map structure to preserve the connection between objects.
  • Data can be fully organized when well organized into different levels.
  • With a Temporal see, you'll keep up a littler see state particularly when data is passed through the browser and gotten back. 

We'll keep holder classes and wrapper classes together but ordinarily awesome to store them freely. When kept autonomous, they are simple to preserve and reuse at whatever point required and it anticipates code duplication too.

Here is an illustration of the wrapper class in lightning component Salesforce. In this case, we are going to make AccConWrapper and will show account and contact details and show in the lightning component.

dont miss out iconDon't forget to check out: 5 Tips for Deploying Custom Objects in Salesforce to Production

Apex Class Controller With the Wrapper Class:

public class AccConController{
    @AuraEnabled
    public static AccConWrapper getAccountWithContacts(String accountId){
        AccConWrapper accWrapper = new AccountContactListWrapper();
        List<Account> accList = [SELECT Id, Name,
            (SELECT Id, FirstName, LastName  From Contacts)
        FROM Account WHERE Id =: accounted];
        System.debug(‘###’+accList );
        if(!accList.isEmpty()){
            accWrapper.accRecord = accList[0];
            accWrapper.contactList = accList[0].Contacts;
            accWrapper.contactCount = accList[0].Contacts.size();
        }
        return accWrapper;
    }     
    // wrapper class with @AuraEnabled and {get;set;} properties 
    public class AccConWrapper {
        @AuraEnabled
        public Account accRecord{get;set;}
        @AuraEnabled
        public List<Contact> contactList{get;set;}
        @AuraEnabled
        public Integer contactCount{get;set;}
    }
}

Lightning Component AccConWrapperExample:

<aura:component controller="AccountContactController" implements="force:hasRecordId">  
    <aura:handler name="init" value="{!this}" action="{!c.initData}"/>
<!--  attribute --->
    <aura:attribute name="accountContactWrapper" type="object"/>
    <aura:attribute name="recordId" type="Id" />
    <div class="slds-p-around--large">
        {!v.accountContactWrapper.accRecord.Name}
        <br/>
        {!v.accountContactWrapper.contactCount}      
    <table class="slds-table slds-table--bordered slds-table--cell-buffer">
        <thead>
            <tr class="slds-text-title--caps">
                <th scope="col">
                    <div class="slds-truncate" title="First Name">First Name</div>
                </th>
                <th scope="col">
                    <div class="slds-truncate" title="First Name">Last Name</div>
                </th>
                <th scope="col">
                    <div class="slds-truncate" title="Email">Email</div>
                </th>
            </tr>
        </thead>
        <tbody>
            <aura:iteration items="{!v.accountContactWrapper.contactList }" var="con">
                <tr>
                    <th scope="row">
                        <div class="slds-truncate" title="{!con.FirstName}">{!con.FirstName}</div>
                    </th>
                    <th scope="row">
                        <div class="slds-truncate" title="{!con.LastName}">{!con.LastName}</div>
                    </th>
                    <th scope="row">
                        <div class="slds-truncate" title="{!con.Email}">{!con.Email}</div>
                    </th>
                </tr>
            </aura:iteration>
        </tbody>
    </table>
    </div>
</aura:component>

dont miss out iconCheck out another amazing blog by Ayush here: Sandboxes in Salesforce - All you Need to Know

Javascript Controller:

({
//After the component is initialized, the doInit action is called in the component's //controller.
    initData: function(component, event, helper) {
      var a= component.get("v.recordId");
      //call apex class method
      var action = component.get('c.getAccountWithContacts');
        action.setParams({
            accountId : component.get("v.recordId")
        });
      action.setCallback(this, function(response) {
        //store state of response
        var state = response.getState();
        if (state === "SUCCESS") {
          //set response value in wrapperList attribute on component.
          component.set('v.accountContactWrapper', response.getReturnValue());
        }
      });
      $A.enqueueAction(action);
    },
})

 

Responses

Popular Salesforce Blogs