Working with Data in Apex
As discussed now it’s the time for some real action, in this series you will learn how to interact with data in your Salesforce org. Before we begin to make sure have created a Salesforce developer org, it is available for free and it is the environment where we will try to implement the Apex we learn here.
- Working with sObjects
- Data Manipulation Language
- SOQL and SOSL Queries
- SOQL For Loops
- sObject Collections
- Dynamic Apex
- Apex Security and Sharing
- Custom Settings
Working with sObjects
sObject is the representation of a row of data in the database and can be a standard (created by Salesforce) or custom (created by user). They are similar to SOAP API, therefore generic sObject abstract type can be used to represent any object. DML works on both generic sObjects and regular sObjects.
Account acc = new Account();
Object__c custObj = new Object__c ();
Casting of sObjects :
sObject s = new Account();
Account a = (Account)s; —– Allowed
Contact c = (Contact)s; —– Not Allowed
*Id of a sObject is read-only and can never be modified.
Accessing sObjects Fields
Fields of sObject are accessible or updateable via the dot notation similar to Java.
Fields which is generated by Salesforce system cannot be modified by the user directly.
Also, the fields which are generated using a formula are also not modifiable.
|Account a = new Account();|
|a.Name = ‘Nameoftheaccount’;
Validating sObjects and Fields
Before execution of the code, the values captured are first parsed against the sObject and if there is any mismatch a run time parse error is encountered.
Salesforce also prevents us from making changes which may result in making some apex code invalid, as mentioned below scenario.
- Changing the API name of a field or object
- Changing the data type of a field
- Deletion of an object or one of its field
- Making OWD(organization wide changes)