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
TehNrdTehNrd 

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.

 

 

Message Edited by TehNrd on 04-09-2009 11:21 AM
Best Answer chosen by Admin (Salesforce Developers) 
TehNrdTehNrd

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

 

Message Edited by TehNrd on 04-10-2009 10:30 AM

All Answers

TehNrdTehNrd

Sheesh I should have looked 5 posts down....

 

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

Ron HessRon Hess

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 ); } }

 


 

TehNrdTehNrd

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.

Message Edited by TehNrd on 04-10-2009 08:34 AM
Ron HessRon Hess

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.

 

 

TehNrdTehNrd

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

}
}

 


 

 

 

Message Edited by TehNrd on 04-10-2009 09:11 AM
TehNrdTehNrd

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

 

Message Edited by TehNrd on 04-10-2009 10:30 AM
This was selected as the best answer
TehNrdTehNrd
The wrapper class solution worked for me! I'll be making a post in the VF board soon to show what I am doing with it.
TehNrdTehNrd

And this is what I needed a dynamic list for.....

 

http://community.salesforce.com/sforce/board/message?board.id=Visualforce&thread.id=11970

jadentjadent
Although there is a workaround (as sited here). This would be a great feature. Please VOTE!

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
Message Edited by jadent on 09-17-2009 10:19 AM