SObject Types

Sobjects are standard or custom objects that stores record data in the database. There is also SObject datatype in apex that is the programmatic representation of these SObjects.

Developers referes to SObject and their fields by their API names.

           EXAMPLE: Account a = new Account();

                        Student__c stu = new Student__c();

“__c” represents custom object/field created by us. By using this “__c” we can differentiate standard object/field and custom object/field.

The following example creates a student with some initial values Name, age and email and assign it to a variable of type Student__c which is a SObject type also.

         Example: Student­­__c st = new Student__c( Name = ‘Rus’ age = ‘20’ email = ‘’);

SObject variable are intilized to null, but can be assigned a valid object reference with the new operator.

EXAMPLE: Account a = new Account ( Name =’ acc1′ BillingCity = Washington); OR         

                     Account a = new Account ();

                    a.Name = Acc1;

                     a.BillingCity = Washington;

Accessing SObject fields:

SObject fields can be accessed or changes with simple dot notation.

  Example: Account acc = new Account ();

                 a.Name = Airway;   //Access the account name field and assign it “Airway”

System generated fields such as created by or last modified by cannot be modified. If we try, the apex runtime engine generates an error.

Additionally, formula fields and values for other fields that are read only for the context user cannot be changed.

Accessing SObject fields through Relationships:

We can create a relationship between object in Salesforce. In Salesforce there are different type of relations available. To understand relationships in Salesforce see this artical Relationships in salesforce

sObject records represent relationships to other records with two fields: an ID and an address that points to a representation of the associated sObject. For example, the Contact sObject has both an AccountId field of type ID, and an Account field of type Account that points to the associated sObject record itself.

The ID field can be used to change the account with which the contact is associated, while the sObject reference field can be used to access data from the account. The reference field is only populated as the result of a SOQL or SOSL query (see note below).

For example, the following Apex code shows how an account and a contact can be associated with one another, and then how the contact can be used to modify a field on the account:


Account a = new Account (name = ‘acc123’);
Insert a; // inserting account record
Contact c = new Contact (LastName = ‘Vincent’);
c.AccountId = a.Id; //associating contact with the account.
Insert c;

//By using below query we can get the account name form contact number.

c = [Select Account.Name from Contact where Id =:];
System.debug(‘Acc name before update:’ + c.Account.Name); // debug log We can also update account from contact.
c.Account.Name = ‘acc123456’;
update c.Account;
System.debug(‘updates acc name’ +; // debug log