+ Start a Discussion

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?
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>
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
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;