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
Kenji778Kenji778 

create sObject of type defined at runtime

Hey all,

Simple syntax question here. When I need to create an sObject to be inserted, but I don't know what kind it will be ahead of time, how can I create an sObject of a certain type given a name? So say the string contact was passed in, how would I create a contact object? Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You can indeed do this, and the good news is there's not too much code involved:

 

sobject mysobj=Schema.getGlobalDescribe().get('Contact').newSObject();

 

All Answers

bob_buzzardbob_buzzard

You can indeed do this, and the good news is there's not too much code involved:

 

sobject mysobj=Schema.getGlobalDescribe().get('Contact').newSObject();

 

This was selected as the best answer
Kenji778Kenji778

Thank you sir, you are awesome. Just what I was looking for!

Ankit AroraAnkit Arora

Try this :

 

String objName = 'contact' ;
sobject sObj = Schema.getGlobalDescribe().get(objName).newSObject() ;
sObj.put('lastName' , 'TestFromCode') ;
insert sObj ;

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Ankit AroraAnkit Arora

Oh no! Not again.. This is a cross post :(

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Shashikant SharmaShashikant Sharma

I think this should help you

 

Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();
Schema.sObjectType sObjType = globalDescription.get(SObjectNameValue);
if(sObj == null)
   { sObj = sObjType.newSObject();}

 here SObjectNameValue can be API Name of any object like it can be 'Account' , 'Contact'' or any custom object like MyCustomObject__c

 

for a example you can also see

http://forceschool.blogspot.com/2011/06/dynamic-binding-when-object-name-is.html

 

in this example I used Dynamic binding where object name was dynamic, you will see I have used 'Account' as hard coded as example but it can be passed by any parameter also .

 

Please ask me if any issue in it.

bob_buzzardbob_buzzard

Snuck in before you Ankit :)  Once again we posted the same thing, obviously on the same wavelength (and have no life, posting on sunday [evening in my case]).

Shashikant SharmaShashikant Sharma

@bob

 

shorter answers are best, I went for too much 

bob_buzzardbob_buzzard

Cross posts galore! I'm going offline so there'll be less for the next few hours :)

bob_buzzardbob_buzzard

@shash - but we all posted the same method - always a good sign that its the correct way!

Ankit AroraAnkit Arora

So true! Life is all about forcedotcom LOL!!

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Shashikant SharmaShashikant Sharma

 

me too ,

@ankit please stay online so there will be some to see issues. :)

 

Ankit AroraAnkit Arora

Funny! Good one Shashikant. I am always there :)

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page