Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Business Analyst
  3. Question
Business Analyst

Use of getter and setter methods for each field in Wrapper classes

Asked by Kobayashi Sasaki Apr 17, 2021 2.9K views 1 answer
Share

About this question

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 getAllOpps() { List listOpp = [SELECT Id, Name ,StageName, CloseDate FROM Opportunity Order By Name asc]; List response = new List(); 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;} } }


Your answer

1 Answer

More Business Analyst discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Business Analyst Blogs

Guides, tips and career advice on Business Analyst from JanBask experts.