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
Zahir BasithZahir Basith 

combining two wrapper class

Hi Dev friends, 

I have a wrapper class with a combination of a custom object<Access__c> and couple of other variables. Now I need have a requirement to setup a similar list (wrapper) with the same set of variables but a different custom object<Asset__c>. end of the day I need a conbined list of all the Access__c records with the rest of parementers and all Asset__c records with the similar set of parements. 
what is the efficient way of doing this? Can I create a simiar wrapper class and combine both to form a third wrapper class? 

Sample of my existing wrapper class : 
    public class AccessWrapper{
        public Access__c accessRecord {get;set;}
        public List<String> contactEmailList {get;set;}

        public String partnerName {get; set;}
        public String endCustomer {get; Set;}
        public Integer numberOfCircuits {get; set;}
        public String plannedWorksEmail {get; set;}

        public AccessWrapper(String partner, String ec, Integer noOfCircuits, String emailAddress){
            this.partnerName = partner;
            this.endCustomer = ec;
            this.numberOfCircuits = noOfCircuits;
            this.plannedWorksEmail = emailAddress;
        }
    }
Next requirement is to have an asset wrapper  and have a thrid wrapper which is combination of AccessWrapper + AssetWrapper. 
    public class AssetWrapper{
        public Asset__c assetRecord {get;set;}
        public List<String> contactEmailList {get;set;}

        public String partnerName {get; set;}
        public String endCustomer {get; Set;}
        public Integer numberOfCircuits {get; set;}
        public String plannedWorksEmail {get; set;}

        public AccessWrapper(String partner, String ec, Integer noOfCircuits, String emailAddress){
            this.partnerName = partner;
            this.endCustomer = ec;
            this.numberOfCircuits = noOfCircuits;
            this.plannedWorksEmail = emailAddress;
        }
    }


Please advice ..
Best Answer chosen by Zahir Basith
PawanKumarPawanKumar
Hi Zahir,
You can use Sobject generic one as below.

public class sObjectWrapper
 {
    public SObject sobjectRecord {get;set;}
    public List<String> contactEmailList {get;set;}

    public String partnerName {get; set;}
    public String endCustomer {get; Set;}
    public Integer numberOfCircuits {get; set;}
    public String plannedWorksEmail {get; set;}

    public AccessWrapper(String partner, String ec, Integer noOfCircuits, String emailAddress){
        this.partnerName = partner;
        this.endCustomer = ec;
        this.numberOfCircuits = noOfCircuits;
        this.plannedWorksEmail = emailAddress;
    }
}

----------------------------------

Inaddition,when you access sobject record in your code that time use
Sobject.get('FIELD_API_NAME');

Instead of Asset.Name directly.

Regards,
Pawan Kumar

PS: Please let me know if it helps you.

All Answers

Srinivasa Chary TaduriSrinivasa Chary Taduri
 public class AccessAssetWrapper
 {
        public Access__c accessRecord {get;set;}
        public Asset__c assetRecord {get;set;}
        public List<String> contactEmailList {get;set;}

        public String partnerName {get; set;}
        public String endCustomer {get; Set;}
        public Integer numberOfCircuits {get; set;}
        public String plannedWorksEmail {get; set;}

        public AccessWrapper(String partner, String ec, Integer noOfCircuits, String emailAddress){
            this.partnerName = partner;
            this.endCustomer = ec;
            this.numberOfCircuits = noOfCircuits;
            this.plannedWorksEmail = emailAddress;
        }
    }
PawanKumarPawanKumar
Hi Zahir,
You can use Sobject generic one as below.

public class sObjectWrapper
 {
    public SObject sobjectRecord {get;set;}
    public List<String> contactEmailList {get;set;}

    public String partnerName {get; set;}
    public String endCustomer {get; Set;}
    public Integer numberOfCircuits {get; set;}
    public String plannedWorksEmail {get; set;}

    public AccessWrapper(String partner, String ec, Integer noOfCircuits, String emailAddress){
        this.partnerName = partner;
        this.endCustomer = ec;
        this.numberOfCircuits = noOfCircuits;
        this.plannedWorksEmail = emailAddress;
    }
}

----------------------------------

Inaddition,when you access sobject record in your code that time use
Sobject.get('FIELD_API_NAME');

Instead of Asset.Name directly.

Regards,
Pawan Kumar

PS: Please let me know if it helps you.
This was selected as the best answer
Zahir BasithZahir Basith
Thanks Srinivasa for your speedy response to my post, I appriciate that :)
Thanks Pawan .. Looks like the solution you gave is more easy to implement in my scenario .. Its worth trying. I will update you on that. 
 
Zahir BasithZahir Basith
Hi Pawan, 

I have already started altering controller to prepare the list of Assets(my new object to the generic wrapper - as you suggested). I have an existng method (which is build to prepare the wrapper list with the existing access__c object). Now I am confused how to use the same method such that I can pass generic sObject to that method and thus the method can handle both Assets__c and Access__c. The reason for my confusion is obviously becuase, the mthod has SOQL calling fields specific to Access__c object right now. Please advice how can I make this method generic such that I can pass both List<Access__c> and List<Asset__c> and it can prepare the wrapper list with a combination of Access__c and Assets__c records. somethng this ...    List<AccessAssetWrapper​>=prepareDisplyList(List<sObject>);

pasting my existing method which is preparing the Access wrapper list.

 public List<AccessWrapper> prepareDisplyList(List<Access__c> accessList){
        String strRole = null;
               if(notification.NotificationType__c == 'PWN'){
                   strRole = PWN_ROLE;
               }
               if(notification.NotificationType__c == 'MSO'){
                   strRole = MSO_ROLE;
               }

               // Build a Map of Access -> Account inorder to identify the Partner for the Access.
             for(Access__c acc : [SELECT Id, Site_Name_B_End__r.End_Customer_Name__r.Account_Name__c FROM Access__c WHERE Id IN: accessList]){
                 accessPartnerMap.put(acc.Id, acc.Site_Name_B_End__r.End_Customer_Name__r.Account_Name__c);
             } 


               // Get the contact Email Address related to the Parent Account of the Access records.
               contactMap = new Map<String, String>();
               partnerContactEmailMap = new Map<String, String>();
              for(Contact con : [SELECT Id, AccountId, Account.Name, Email, Role__c, Left_the_Company__c FROM Contact WHERE Role__c INCLUDES(:strRole) AND Left_the_Company__c <> True AND AccountId IN: accessPartnerMap.values()]){

                if(!contactMap.containsKey(con.AccountId)){
                    contactMap.put(con.AccountId, con.Email+'; ');
                }else{
                    String email = contactMap.get(con.AccountId);
                    contactMap.put(con.AccountId, email + con.Email + '; ');
                }    

                if(!partnerContactEmailMap.containsKey(con.AccountId)){
                    partnerContactEmailMap.put(con.AccountId, con.Email+'; ');
                }else{
                    String email = partnerContactEmailMap.get(con.AccountId);
                    partnerContactEmailMap.put(con.AccountId, email + con.Email + '; ');
                }                            
               }

            display_list = new List<AccessWrapper>(); //set the display_list object to a new AccessWrapper List
            AggregateResult[] aggResult = [SELECT COUNT(Id) noOfCircuits, 
                                                Site_Name_B_End__r.End_Customer_Name__r.Account_Name__c PartnerId, 
                                                Site_Name_B_End__r.End_Customer_Name__r.Account_Name__r.Name Partner, 
                                                Site_Name_B_End__r.End_Customer_Name__c ECId,
                                                Site_Name_B_End__r.End_Customer_Name__r.Name EC 
                                            FROM Access__c 
                                            WHERE Id IN:accessList
                                            GROUP BY Site_Name_B_End__r.End_Customer_Name__r.Account_Name__c, 
                                                Site_Name_B_End__r.End_Customer_Name__r.Account_Name__r.Name, 
                                                Site_Name_B_End__r.End_Customer_Name__c,
                                                Site_Name_B_End__r.End_Customer_Name__r.Name];


            for(AggregateResult agg : aggResult) {
                display_list.add(new AccessWrapper(String.valueOf(agg.get('Partner')), String.valueOf(agg.get('EC')), Integer.valueOf(agg.get('noOfCircuits')), contactMap.get(String.valueOf(agg.get('PartnerId')))));
            }

            if(display_list!= NULL){
                return display_list;
            }else{
                return null;
            }
    }

-----------------------------------------------------------------
Now I need to pass  List<sObject> to the above method, how do I handle that when the feilds are different in the SOQL (the difference is only in the places where it is marked as bold.) .. please advice 

Many thanks in advance.