Is there a way to instantiate an SObject?
I have done the following:
Id objId = 'a0Ci000000vd7xA';
String objName = ((Id)objId).getSobjectType().getDescribe().getName();
Type t = Type.forName(objName); //object name
Sobject myObj = (Sobject)t.newInstance();
myObj.emp_name__c = 'test';
Field expression not allowed for generic SObject
Best Answer
You don't need to go through the complicated method you went through to get the new record. The following works just as well:
This approach also has the advantage that it also works even if someone does something silly like creating a class called "Account" and you try to instantiate an Account record.
If you want to use the static methods, you have to first cast to a concrete type:
However, if you want to use the object generically, you use the get/put SObject methods: