+ Start a Discussion
sagar patil 13sagar patil 13 

How To Create Generic Class for SObject In apex code

Using a Sobject name(which will be String) I should able to access fields of that SObject.
Alex SelwynAlex Selwyn
You can use sObject describe to get all the field names of an sObject. If thats what you are looking for.

Hi sagar,

Please try the below code,
String sObjectName = 'Contact';
Schema.SObjectType t = Schema.getGlobalDescribe().get(sObjectName);
SObject s = t.newSObject();
s.put('LastName', 'TestGeneric');
insert s;

Hope this helps.