Salesforce: Instantiating an SObject Dynamically at Run-time
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.