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 wrapperslist into single list

Hi,
I have written three seperate wrapper classes for three different Sobjects. I need to add three wrappers into single list. Please help me how to do
---------------------------------------------
@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);
    //    s.add(wrapContact);
    //    s.add(wrapAccount);
    //    s.add(wrapLead);
     List<SObject> s = new List<SObject>{
            new Contact(),
            new Account(),
            new Lead()            
        };
   //         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 am approaching in this way, but addAll method is not working
 List<sObject> objects = new List<sObject>();
        objects.addAll((List<sObject>)(wrapContact));
          objects.addAll((List<sObject>)(wrapAccount));
objects.addAll((List<sObject>)(wrapLead));
------------------------------------------------------
User-added imageI need to add below three lists into single list.
User-added image
PriyaPriya (Salesforce Developers) 
Hi Abhilash,

Can you kindly check this link if this what you are looking for :-

https://salesforce.stackexchange.com/questions/349736/how-can-i-add-different-objects-in-one-list
 

Kindly mark it as the best answer if it works for you.

 

Thanks & Regards,

Priya Ranjan