Wrapper Class in Java | The Salesforce Developer Guide : Ayush

Wrapper Class in Java | The Salesforce Developer Guide
by: Ayush
blow post content copied from  Forcetalks
click here to view original post

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{
    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 );
            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 {
        public Account accRecord{get;set;}
        public List<Contact> contactList{get;set;}
        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">
    <table class="slds-table slds-table--bordered slds-table--cell-buffer">
            <tr class="slds-text-title--caps">
                <th scope="col">
                    <div class="slds-truncate" title="First Name">First Name</div>
                <th scope="col">
                    <div class="slds-truncate" title="First Name">Last Name</div>
                <th scope="col">
                    <div class="slds-truncate" title="Email">Email</div>

Lightning Component AccConWrapperExample:-            

            <aura:iteration items="{!v.accountContactWrapper.contactList }" var="con">
                    <th scope="row">
                        <div class="slds-truncate" title="{!con.FirstName}">{!con.FirstName}</div>
                    <th scope="row">
                       <div class="slds-truncate" title="{!con.LastName}">{!con.LastName}</div>
                    <th scope="row">
                        <div class="slds-truncate" title="{!con.Email}">{!con.Email}</div>

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');
            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());


The post Wrapper Class in Java | The Salesforce Developer Guide appeared first on Forcetalks.

December 04, 2020 at 11:46PM
Click here for more details...

The original post is available in Forcetalks by Ayush
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.