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
ToddKruseToddKruse 

possible to combine two or more list objects?

I have to retrieve possibly more than 10,000 rows in an SOQL query.  I thought what I could do was create multple SOQL queries that would return parts of the data and then take each of those lists and combine it into one big list since the Spring 10 release has removed the limits on list objects.

 

I have created a master list, and then did for each loops with different soql queries to populate that master list, but was hoping there was a cleaner way of doing it.

 

Thanks

 

--Todd Kruse

 

Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae

You could use the addall method as a more efficient way to combine your sublists, instead of the for loops.

 

 

List<Lead> L1 = new List<Lead>([select id,name,leadsource from Lead where leadsource='White Paper']); List<Lead> L2 = new List<Lead>([select id,name,leadsource from Lead where leadsource='Inbound']); List<Lead> MasterLead = new List<Lead>(); MasterLead.addall(L1); MasterLead.addall(L2);

 

 

 

 

 

All Answers

JimRaeJimRae

You could use the addall method as a more efficient way to combine your sublists, instead of the for loops.

 

 

List<Lead> L1 = new List<Lead>([select id,name,leadsource from Lead where leadsource='White Paper']); List<Lead> L2 = new List<Lead>([select id,name,leadsource from Lead where leadsource='Inbound']); List<Lead> MasterLead = new List<Lead>(); MasterLead.addall(L1); MasterLead.addall(L2);

 

 

 

 

 

This was selected as the best answer
ToddKruseToddKruse

That worked great!  Only one issue now.  When I insert records into the masterList, they need to be resorted after everything is done.  Is there a way to re-order the list so its in the correct order?

 

Thanks

 

--Todd Kruse 

JimRaeJimRae

Really depends on what the list is. If it is a primative list, you could sort it directly with the sort method.

If it is a list of sObjects, like in my sample code, you can't sort it directly, you will need to do some additional processes to sort the list, depending on what you want to sort on, and how tight you need the sorting to be.  For example, if you are sorting by a decimal value, how do you want to handle duplicate "key" values?  do you need to then sort by some secondary key, or would it be OK to put them out in whatever order (inside the duplicate key) they come in.  What type of field do you want to sort by?  Maybe I can whip up an example.

 

 

ToddKruseToddKruse

I am creating 4 lists.  Each list is grabbing records from a table for a quarter of the year.

 

an example of one of the lists:

 

Select id, name, Unique_Role_Name__c, Role__c, Month_Total__c, Assignment__c, Total_Date__c, Project__c

From SFDC_ScheduleGroup__c 

Where Total_Date__c >: startQ2

And Total_Date__c <: endQ2

And Project__c <> null

Order By Project__c Desc, Unique_Role_Name__c, Month_Total__c]

 

so I need to sort it first by Project__c and then by Unique_Role_Name__c.

 

the masterList that is created has all of these records out of order.

 

Thanks

 

 

--Todd Kruse 

JimRaeJimRae

A two level sort is a bit more complicated.

Here is some sample code I found useful, you might be able to leverage it:

http://community.salesforce.com/sforce/board/message?board.id=apex&message.id=11655

 

If I have time, I will try to put a simple example of a single level sort together for you.

Maybe someone else has done this and could post a code snippet for you to leverage.