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
someonesomeone 

Cast List<Case> to List<Object>

I have a search function that should query for a List<Cases>, and then a List<Opportunities>, then merge both collections and return the final list as a JSON serialized string. Apex SOQL does not allow me to query for a List<Object> to make the lists more generic before merging them. How can I get around this? Should I handle both lists individually, then merge them after they been serialized to string?
someonesomeone
I changed the two lists to be of type List<SObject> and then tried to merge them using listOfCases.addAll(listOfOpps) but that fails with exception:
 Collection store exception adding all LIST<Opportunity> to LIST<Case>
pconpcon
Should be able to do something like
 
List<Object> objects = new List<Object>();
objects.addAll((List<Object>)([select ... from Case]));
objects.addAll((List<Object>)([select ... from Opportunity]));

then act on your object list
BalajiRanganathanBalajiRanganathan
why dont you create a wrapper object, set your lists to this wrapper and create json out of the wrapper object.
 
public class yourwrapper {
 private List<Case> caseList {set;get;}
 private List<Opportunity> oppList {set;get;}
 
 public yourwrapper (List<Case> caseList, List<Opportunity> oppList) {
  this.caseList = caseList;
  this.oppList = oppList;
 }

}