+ 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.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_describe_objects_understanding.htm
EldonEldon
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.
Regards