function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sahil Sharma 36Sahil Sharma 36 

Hi, Why are we able to refer 'Sobject' list to 'Account' list.

List<Account> accList = new List<Account>();
        accList.add(new Account(name = 'ss'));
   
        List<SObject> sObjectList = accList;
        accList = sObjectList;    
        
                        or
        
        List<Account> aList = new List<SObject>();
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Sahil Sharma,
  • Salesforce Object Search Language (SOSL) is a Salesforce search language that is used to perform text searches in records.
  • This is an example of an SOSL query that searches for accounts and contacts that have any fields with the word 'SFDC'.
  • List<List<SObject>> searchList = [FIND 'SFDC' IN ALL FIELDS 
                                          RETURNING Account(Name), Contact(FirstName,LastName)];

    Please refer the below link for reference from Trailhead.
  • https://trailhead.salesforce.com/en/modules/apex_database/units/apex_database_sosl
Hope it will be helpful.

Thanks
Rahul
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for Sobject
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm

sObject methods are all instance methods, that is, they are called by and operate on a particular instance of an sObject, such as an account or contact.

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.
 
Let me know if you still have any doubts.

 
Sahil Sharma 36Sahil Sharma 36
Thanks for the answers guys. However i still don't understand how the reference of Account and contact class are able to store the object of SObject. SObject is the parent class and Account is child a class. Therefore the above scenario means downcasting which is not allowed in apex.