+ Start a Discussion

Dynamic Set or List type creation

Is there a way to instantiate a List when the type is not known?


I have the type of the field but need to create a list of that specific type, since the type may change I would like to be able to declare the List type dynamically.


The reason is the List will be used in a query and I cannot simply use the List<Object> in the query.....


1. I have the Schema.DisplayType of the field

2. I need to create a list of that displaytype and I do not wish to hardcode the type


If I use the IN: Operation in the SOQL query it errors when using a list of type Object.

If I try to convert the field to string using string.valueOf then the query errors when the field I am comparing is not a string.


Anyone have any ideas? 


btw, it is assumed that the field I am comparing in the query before the IN is the same type as the field in the list that I am binding to


You can use  list<sobject>


SRK wrote:

You can use  list<sobject>



Get the error

Invalid conversion from runtime type String to SObject

I am using an sobject.get('FIELDNAME') to populate the List. I need the List<DYNAMIC> so that I can construct the List based on the previously identified by Schecm.DisplayType for the FIELDNAME for which I am getting.


Certainly thing is possible somehow.



When I use List<Object> I can construct the List and populate it bu then I cannot use that list in an DOQL for the IN parameter