Apex Basic and Database :Using SObject
Note: All credit goes to Salesforce, I am just a developer who enjoy playing trailhead and try to make note to myself.
Relationship between Sobjects and Salesforce record
Every record in Salesforce is natively represented as an sObject in Apex. For example, the Acme account record corresponds to an Account sObject in Apex. The fields of the Acme record that we can view and modify in the user interface can be read and modified directly on the sObject as well.Each Salesforce record is represented as an sObject before it is inserted into Salesforce. Likewise, when persisted records are retrieved from Salesforce, they’re stored in an sObject variable.
Some rules used for API names for custom objects and custom fields.
For custom objects and custom fields, the API name always ends with the __c suffix. For custom relationship fields, the API name ends with the __r suffix. For example:
- A custom object with a label of Merchandise has an API name of Merchandise__c.
- A custom field with a label of Description has an API name of Description__c.
- A custom relationship field with a label of Items has an API name of Items__r.
Generic sObject datatype
If we don’t know the type of sObject that our method is handling, we can use the generic sObject data type.We also can can cast Sobject to specific sObject type.Specify sObject type will allow us to use dot notation.
But variables that are declared with the specific sObject data type can reference only the Salesforce records of the same type.
Generic sObjects can be created only through the newSObject() method.
Also, the fields of a generic sObject can be accessed only through the put() and get() methods.
We can use dot notation in specific sObject type.