What Is sObjects in Salesforce?
Unlike any other programming language like Java or C#, Apex is tightly integrated with the database. Hence we do not have to create any database connection to access the records or insert new records. Instead, in Apex, we have sObjects which represent a record in Salesforce.
An account record named Burlington Textiles in Apex will be referred to using a sObject, like this:
Account acc = new Account(Name=’Disney’);
The API object name becomes the data type of the sObject variable in Apex.
Account = sObject datatype
acc = sObject variable
new = Keyword to create new sObject Instance
Account() = Constructor which creates an sObject instance
Name =‘Disney’ = Initializes the value of the Name field in account sObject
Similarly, if we want to create a contact record using Apex then we first need to create a sObject for it in Apex, like this:
Contact con = new Contact();
Now there are 2 ways to assign field values to the contact sObject:
1. Through Constructor
Contact con = new Contact(firstName = ‘Shrey’, lastName = ‘Sharma’);
2. Using Dot Notation
Contact con = new Contact(); con.firstName = ‘Shrey’; con.lastName = ‘Sharma’;
Similarly, for Custom Objects:
Student__c st = new Student__c(Name = ‘Arnold’);
If we want to assign the field values for custom fields then also we have to write down their field API name, like:
- For Standard Object:
Account acc = new Account(Name = ‘Disney’, NumberOfLocations__c = 56);
- For Custom Object:
Student__c st = new Student__c(Name = ‘Arnold’, Email__c = ‘arnold @gmail.com’);