+ Start a Discussion
ANAMIKA MONDAL 8ANAMIKA MONDAL 8 

generic sobject

Can someone please explain me what is generic object? and why we use it?
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Anamika,

Generic sObject means, it can be of any persisted SFDC objects type.
For ex: Vehicle is generic type and Car, Motor Bike all are concrete types of Vehicle.
In SFDC,sObject is generic and Account, Opportunity, CustomObject__c are its concrete type.
Generic sObject abstracts the actual representation of any object assigned to it. sObject is the name of the generic abstract type that can be used to represent any persisted object type.

Please refer the below link for reference. Hope it will be helpful.

Please mark it as best answer if the information is informative.

Thanks
Rahul Kumar
 
Waqar Hussain SFWaqar Hussain SF
Generic sObject is a general type of sObject, just like Account, Contact and custom object. 
 
List<SObject> GenericList = new List<SObject>();
SObject acc = [Select Id, Name from Account limit 1];
GenericList.add(acc);

SObject con = [Select Id, Name from Contact limit 1];
GenericList.add(con);

system.debug(GenericList);

See below article for more information. 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm

Let me know, If you have any confusion. 

Regards,
Waqar Hussain 
Skype: waqar.hussain991
2011waqar@gmail.com 
Ravi Dutt SharmaRavi Dutt Sharma
Its like a parent object for all the standard and custom objects we have  in Salesforce. Generally, we use sObject when we write dynamic code in which we do not know which object we would be processing at run time. For example, lets say we have a general method which can work for both Account and Contacts and it accepts the account / contact records a param. In this case, we can generalise the method parameter as sObject like below:
 
public void processData(sObject sobj){
        
        if(sObject instanceOf Account){
              Account acc = (Account) sobj;
        }else if(sObject instanceOf Contact){
              Contact con = (Contact)sobj;       
        }

}