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
MiddhaMiddha 

Dynamic APEX - creating object list at runtime

Hi ,

 

I have a requirement to add sharing records for all the Account child object. For this i used Describe to get the sharing object for all the Account child objects.

 

List<Schema.ChildRelationship> childRel = objDef.getChildRelationships();

 

SO, if i am getting 5 share objects from this, and say 100 child records which needs sharing to be added, how do i create a List for these 5 objects so that i can add these records into the list and use insert.

 

List<sObject> l = new List<???>();

 

What should be the correct syntex to create different lists for different objects at runtime. Please suggest.

 

Thank you!!

TehNrdTehNrd
I'd like to know this as well...... http://community.salesforce.com/sforce/board/message?board.id=apex&message.id=14662&jump=true#M14662
Message Edited by TehNrd on 04-09-2009 11:08 AM
Ron HessRon Hess

looks like you cannot create a dynamic list.

 

you can run a query or SOSL string to get a list of objects

 

List<List <sObject>> myQuery = search.query(SOSL_search_string);

 

LaurentDLaurentD

Hi.

 

I also came across this problem. Indeed a SOQL or SOSL will return a list of SObject, but a SOQL will throw a QueryException if nothing returns.

Then our List<SObject> will still be null, and it will be impossible to use it.

But a SOSL returning nothing actually instatiates the List:

 

 

 

String searchquery='FIND\'xxx\'IN ALL FIELDS RETURNING '+ objectType +'(name)';List<List<SObject>>searchList=search.query(searchquery);List<SObject> listSkills = searchlist[0];

 Then you can use your list and add the object you want.

 

Cheers 

 

 

XactiumBenXactiumBen

I think this is the one thing missing from dynamic apex - the ability to create SObject or SObject lists.

 

In the ajax toolkit you can use new sforce.SObject(string type), it seems to me like we need a database.create() method and a database.createList() method in Apex - Either passing in a string or, even better, passing in an SObjectType token.  At the moment I have had to create a static createSObject method with lots of if statements that returns different objects depending on what string is passed in - not very nice at all and a pain if I want to add support for additional objects.

 

Anyone know if there is an idea posted for this functionality?

jadentjadent
Although there is a workaround by creating a wrapper object. This would be a great feature. Please VOTE!

Creating SObject List types at runtime:

Creating non-concrete SObject lists: