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
Abilash.SAbilash.S 

How to add multiple wrapper list into single list

Hi Everyone,

I have created apex class to pull multiple object records into single list. Where objects does not have any relationship (lets say account, contact, lead. Consider account and contact dont have any relationship). I used for loop seperately for each object and saved into wrapperlist. For threee objects I have three wrapper list. Now big challenge is I need to add three wrappers(wrapAccount, wrapContact, wrapLead)  into single list. So in that list I need all three objects records.

-------------------------------------------------------------------------
@Auraenabled(cacheable=true)  
    public static List<sObject> wrapData() {
        List<WrapperContact> wrapContact = new List<WrapperContact>();
        List<WrapperAccount> wrapAccount = new List<WrapperAccount>();
        List<WrapperLead> wrapLead = new List<WrapperLead>();
        
       
        
        for(Contact ct : [select id, name from Contact LIMIT 10]){          
            wrapContact.add(new WrapperContact(ct));            
        }
        for(Account acct : [select id, name from Account LIMIT 10]){          
            wrapAccount.add(new WrapperAccount(acct));            
        }
        for(Lead ld : [select id, name from Lead LIMIT 10]){          
            wrapLead.add(new WrapperLead(ld));            
        }
        system.debug('wrapContact'+wrapContact);
        system.debug('wrapAccount'+wrapAccount);
        system.debug('wrapLead'+wrapLead);
     List<SObject> s = new List<SObject>{
            new Contact(),
            new Account(),
            new Lead()            
        };
   //     s.add(wrapContact);
   //     s.addAll(wrapAccount);
   //         system.debug(s);
        List<sObject> objects = new List<sObject>();
    //    objects.addAll((List<sObject>)(wrapContact));
      //    objects.addAll((List<sObject>)(wrapAccount));
   //     objects.addAll(wrapLead);
          return objects;
     
    }



     public class WrapperAccount{
                
        @Auraenabled
        public Account ac{get;set;}
         
        public WrapperAccount(Account acct){
            ac=acct;
        }
        
    }

     public class WrapperContact{
        @Auraenabled
        public Contact cont{get;set;}
              
        public WrapperContact(Contact ct){
            cont=ct;
        }
                      
    }

     public class WrapperLead{
               
        @Auraenabled
        public Lead ldd{get;set;}
            
        public WrapperLead(Lead ld){
            ldd=ld;
        }
    }    

----------------------------------------------------------------------
I have used List<SObject> s = new List<SObject>{
            new Contact(),
            new Account(),
            new Lead()            
        };
   //     s.add(wrapContact);
   //     s.addAll(wrapAccount);
         s.addAll(wrapLead);
   //         system.debug(s);
But its not working. Please help me out of this.
Thanks in Advance
Best Answer chosen by Abilash.S
Sumit Mishra 47Sumit Mishra 47

Create an additional class 'WrapperSobject' extend all your other class WrapperLead,WrapperContact, etc . This concept is known as inheritance, sharing an example below to make it more clear.

 

 

/**
 * @description       :
 * @author            : Sumit Mishra
 * @group             :
 * @last modified on  : 10-19-2022
 * @last modified by  : Sumit Mishra
**/
public with sharing class MyExample {
    public MyExample(){
        List<WrapperObject> woList = new List<WrapperObject>();
        woList.add(new WrapperAccount([Select Id from Account Limit 1]));
        woList.add(new WrapperContact([Select Id from Contact Limit 1]));
    }

    public abstract class WrapperObject{}
    public class WrapperAccount extends WrapperObject{
               
        @Auraenabled
        public Account ac{get;set;}
         
        public WrapperAccount(Account acct){
            ac=acct;
        }
       
    }
     public class WrapperContact  extends WrapperObject{
        @Auraenabled
        public Contact cont{get;set;}
             
        public WrapperContact(Contact ct){
            cont=ct;
        }
                     
    }
     public class WrapperLead extends WrapperObject{
               
        @Auraenabled
        public Lead ldd{get;set;}
           
        public WrapperLead(Lead ld){
            ldd=ld;
        }
    }  
}
 

All Answers

Sumit Mishra 47Sumit Mishra 47

Create an additional class 'WrapperSobject' extend all your other class WrapperLead,WrapperContact, etc . This concept is known as inheritance, sharing an example below to make it more clear.

 

 

/**
 * @description       :
 * @author            : Sumit Mishra
 * @group             :
 * @last modified on  : 10-19-2022
 * @last modified by  : Sumit Mishra
**/
public with sharing class MyExample {
    public MyExample(){
        List<WrapperObject> woList = new List<WrapperObject>();
        woList.add(new WrapperAccount([Select Id from Account Limit 1]));
        woList.add(new WrapperContact([Select Id from Contact Limit 1]));
    }

    public abstract class WrapperObject{}
    public class WrapperAccount extends WrapperObject{
               
        @Auraenabled
        public Account ac{get;set;}
         
        public WrapperAccount(Account acct){
            ac=acct;
        }
       
    }
     public class WrapperContact  extends WrapperObject{
        @Auraenabled
        public Contact cont{get;set;}
             
        public WrapperContact(Contact ct){
            cont=ct;
        }
                     
    }
     public class WrapperLead extends WrapperObject{
               
        @Auraenabled
        public Lead ldd{get;set;}
           
        public WrapperLead(Lead ld){
            ldd=ld;
        }
    }  
}
 

This was selected as the best answer
Abilash P 1Abilash P 1
Hi Sumit,
Thanks for response. Its working fine but its returning only single record from each object. Ofcourse we are given LIMIT 1. If i tried more than one getting query exception.

How to achieve multiple records. Thats why I have used for loop. But each stores in different list. How to add different lists (wrapAccount, wrapContact, wrapLead) into single list. 

Thanks in Advance
Abilash.SAbilash.S
Ok I have used addAll method. Its working now. Getting multiple records from different objects.

@Auraenabled(cacheable=true)  
    public static List<WrapperObject> wrapData() {
        List<WrapperContact> wrapContact = new List<WrapperContact>();
        List<WrapperAccount> wrapAccount = new List<WrapperAccount>();
        List<WrapperLead> wrapLead = new List<WrapperLead>();
        List<WrapperObject> woList = new List<WrapperObject>();
       
        for(Contact ct : [select id, name from Contact LIMIT 2]){          
            wrapContact.add(new WrapperContact(ct));            
        }
        woList.addAll(wrapContact);
        system.debug('woList1 '+woList);
   
        for(Account acct : [select id, name from Account LIMIT 2]){          
            wrapAccount.add(new WrapperAccount(acct));            
        }
        woList.addAll(wrapAccount);
        system.debug('woList2 '+woList);
        
        for(Lead ld : [select id, name from Lead LIMIT 2]){          
            wrapLead.add(new WrapperLead(ld));            
        }
        woList.addAll(wrapLead);
        system.debug('woList3 '+woList);
  
     return woList;
    }    


public abstract class WrapperObject{}
     public class WrapperAccount extends WrapperObject{
                
        @Auraenabled
        public Account ac{get;set;}
         
        public WrapperAccount(Account acct){
            ac=acct;
        }
        
    }

     public class WrapperContact extends WrapperObject{
        @Auraenabled
        public Contact cont{get;set;}
              
        public WrapperContact(Contact ct){
            cont=ct;
        }
                      
    }

     public class WrapperLead extends WrapperObject{
               
        @Auraenabled
        public Lead ldd{get;set;}
            
        public WrapperLead(Lead ld){
            ldd=ld;
        }
    }