0

I understand that wrapper class is like a custom object that you can define in an Apex class but I am not sure why getter and setter methods are used for each field in a wrapper class. What is the purpose of it?

 public with sharing class GetAllOpportunities {
       @AuraEnabled(cacheable=true)
        public static List<DataTableWrapper> getAllOpps() {
            List<Opportunity> listOpp = [SELECT Id, Name ,StageName, CloseDate 
                                         FROM Opportunity Order By Name asc];
    
            List<DataTableWrapper> response = new List<DataTableWrapper>();
            for(Opportunity opp : listOpp){
                DataTableWrapper obj = new DataTableWrapper();
                obj.oppId = opp.Id;
                obj.name = opp.Name;
                obj.nameUrl = '/'+opp.Id;
                obj.stageName = opp.StageName;
                obj.closeDate = opp.CloseDate;
                response.add(obj);
            }
    
            return response;
        }
    
        private class DataTableWrapper {
            @AuraEnabled
            public Id oppId {get;set;}
            @AuraEnabled
            public String name {get;set;}
            @AuraEnabled
            public String nameUrl {get;set;}
            @AuraEnabled
            public String stageName {get;set;}
            @AuraEnabled
            public Date closeDate {get;set;}
        }
    
    
    }

1 Answer 1

0

Wrapper Class in Apex Salesforce : A wrapper or container class is a class, a data structure, or an abstract data type which contains different objects or collection of objects as its members.

A wrapper class is a custom object defined by programmer wherein he defines the wrapper class properties. Consider a custom object in salesforce, what do you have in it? fields right? different fields of different data types. Similarly wrapper class is a custom class which has different data types or properties as per requirement. We can wrap different objects types or any other types in a wrapper class.

Below is the code for your reference

APEX-

public inherited sharing class LWCExampleController {

    @AuraEnabled(Cacheable = true)
    public static List<WrapperDemo> fetchAccAndCons() {
        List<WrapperDemo> lstWrapper = new List<WrapperDemo>();

        for(Account acIterator : [ SELECT Id, Name, (Select Id, Name From Contacts) FROM Account WHERE Id = '001B000000vZWOHIA4'] ) {
            lstWrapper.add(new WrapperDemo(acIterator, acIterator.Contacts));
        }

        return lstWrapper;

    }

 // Wrapper Class
 public class WrapperDemo {
        @AuraEnabled public Account objAcc;
        @AuraEnabled public list<Contact> lstCons;

        public WrapperDemo(Account acc, list<Contact> lstCons) {
            this.objAcc = acc;
            this.lstCons = lstCons;
        }
    }
}

3
  • Thank you. Do you know why we use getter setter methods for each field in a wrapper class enclosed in a private method? I have edited to include the code. Commented Apr 14, 2020 at 6:02
  • If you are capturing the data from UI or from external interfaces and want to store the those attibute values inside a class, you have to define them as attributes. getter - returning value of the attribute setter - assigning value to that attribute Commented Apr 14, 2020 at 6:31
  • Basically I am just trying to display a lightning datatable in UI with Id as a URL. In that case is it needed? Commented Apr 14, 2020 at 7:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .