The Silver Lining

Lessons & Learnings from a salesforce certified technical architect.

Posts Tagged ‘instantiate

Salesforce: Instantiating an SObject Dynamically at Run-time

with 13 comments

I’m sure a lot of you have this documented somewhere but I’ve recently discovered that it’s quite difficult to find an obvious reference to this knowledge on the interwobbles. So how would you create a Generic SObject at run-time? It’s rather easy thankfully:

String sObjectName = 'MyObject__c';
Schema.SObjectType t = Schema.getGlobalDescribe().get(sObjectName);
SObject s = t.newSObject();

If you require this type of functionality quite often I’d suggest putting it in a utility class.

Advertisements

Written by Wes

January 25, 2011 at 8:54 pm

%d bloggers like this: