You need to sign in to do that
Don't have an account?
Is it possible to declare dynamic lists?
Id like to know if anyone as figured out a way to dynamically declare a list at run time. I'm trying to create a super utilty class that can take a List of sObject and process them but in my logic I need to create a list dynamically based on the sObject type. Is this possible? I hope so.
This is a simple test to demonstrate what I am trying to achieve.
sObject ob = new Opportunity(Name = 'test');
Schema.sObjectType obType = ob.getSObjectType();
List<sObject> resultList = new List<obType>();
Thanks.
Gotta love the wrapper class!
Not sure if this will work yet but it's definitely a step forward. I'm basically tricking the system and creating a list that is List<sObject> myList = new List<sObject>();
public static void processList(List<sObject> items){
List<cObject> resultList = new List<cObject>();
for(sObject s : items){
resultList.add(new cObject(s));
}
}
public class cObject{
sObject obj {get; set;}
public cObject(sObject obj){
this.obj = obj;
}
}
All Answers
Sheesh I should have looked 5 posts down....
http://community.salesforce.com/sforce/board/message?board.id=apex&thread.id=14642
i may have mis-understood
is this correct?
public class foo { void ff() { sObject ob = new Opportunity(Name = 'test'); Schema.sObjectType obType = ob.getSObjectType(); List< Schema.sObjectType > resultList = new List< Schema.sObjectType >(); resultList.add( obType ); } }
Not exactly. I would want the declared list to be one that contains Opportunities.
sObject ob = new Opportunity(Name = 'test');
Schema.sObjectType obType = ob.getSObjectType();
List< Schema.sObjectType > resultList = new List< Schema.sObjectType >();
//resultList should be equivalent to List<Opportunity> resultList = new List<Opportunity>();
Looking at your post in the other thread is seems like this is not possible. Bummer.
when i tried the error message indicated that the sobject type must be concrete.
So you can create a list of opportunities, but not a list of AnyType.
Correct. I can do this:
List<sObject> opps = new List<Opportunity>();
Unfortunately in my example my class method is receiving a list of sObjects where the type of the object is unknown so I can not hardcode my declaration of the result list. I can inspect the list and determine the type of objects in the list but I can not declare a list based on the type of the objects.
Here is an expanded example of what I am trying to achieve.
public class myClass {
public static void processList(List<sObject> items){
//Identify the sObject type of the list
sObject ob = items[0];
Schema.sObjectType obType = ob.getSObjectType();
//Now I need to dynamically declare a list based on the items list, obType is 'Opportunity'
List<obType> resultList = new List<obType>();
}
public static testMethod void myTest(){
List<Opportunity> opps = new List<Opportunity>();
for(integer i = 0; i<25; i++){
opps.add(new Opportunity(Name = 'test' + i, Amount = 1000 * Math.random()));
}
List<sObject> genericObjects = new List<Opportunity>();
for(Opportunity o : opps){
genericObjects.add(o);
}
processList(genericObjects);
}
}
Gotta love the wrapper class!
Not sure if this will work yet but it's definitely a step forward. I'm basically tricking the system and creating a list that is List<sObject> myList = new List<sObject>();
public static void processList(List<sObject> items){
List<cObject> resultList = new List<cObject>();
for(sObject s : items){
resultList.add(new cObject(s));
}
}
public class cObject{
sObject obj {get; set;}
public cObject(sObject obj){
this.obj = obj;
}
}
And this is what I needed a dynamic list for.....
http://community.salesforce.com/sforce/board/message?board.id=Visualforce&thread.id=11970
Creating SObject List types at runtime:
http://ideas.salesforce.com/article/show/10098136/Dyanmic_APEX__Creating_an_SObject_List_at_Runtime
Creating non-concrete SObject lists:
http://ideas.salesforce.com/article/show/10093259