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
Mike LeachMike Leach 

Create Generic sObjects in Apex?

Is there a way to create generic sObjects in Apex? I'm looking for an equivalent to this:

string lateBoundType = 'MyCustomObject__c';

 

sObject obj = new sObject();

obj.Type = lateBoundType;

 

XmlDoc fields = //Create fields XML here

obj.Any = fields;

 

obj.Create();

 

Thanks!

 

-Mike

 

 

 

bob_buzzardbob_buzzard

Not according to the Apex Code Developer's Guide - page 30 states:

 

 

The new operator still requires a concrete sObject type, so all instances are specific sObjects. 

WesNolte__cWesNolte__c

Hey

 

Does this help?

 

 

string lateBoundType = 'MyCustomObject__c';

 

sObject obj = Schema.getGlobalDescribe().get(lateBoundType).newSObject();

 

XmlDoc fields = //Create fields XML here

 

obj.Any = fields;

obj.Create();

 

 Wes

 

Message Edited by weznolte on 01-19-2010 11:43 AM
Message Edited by weznolte on 01-19-2010 11:44 AM
Mike LeachMike Leach

I'll give this a try. Thanks!

 

-Mike