Salesforce sObject Types
Salesforce sObject, can be a generic SObject or be a specific Subjects, such as an Account, Contact, or MYcustom__c. sObjects (short for “Salesforce Objects”) are standard or custom objects that stores record data in the Force.com database.There is also an sObject data type in Apex that is the Programmatic representation of these sObjects and their data in code. Developers refer to SObjects and their fields by their API names.
Example:-
Account a= new Account (); MyCustomObject__c co= new MYCusomobject__c (); MyCustomObject__c is an API name of Custom Object
The following example creates an invoice statement with some initial values for the description__c Fields and assigns it to variables of type Invoice-Statement__c, which is a SObject type also.
Example:-
Invoice-statment__c inv- new Invoice-statement__c(Description__c= 'Test Invoice', Status__c='pending')
SObject Variables are initialized to null, but can be assigned a valid object reference with the new operator.
Example:-
Account a = new Account();Account a= new Account (Name='Tutorialkart'), billingcity= 'Hyderabad');
( or )
Account a= new Account (); a.name='TutorialKart'; a.BillingCity= 'Hyderabad';
Accessing sObject Fields
sObject fields can be acccessed or changes with simple dot notation.
Example:-
Account a = new Account (); a.Name='Salesforcetutorial.com'; Access the account name field and assign it 'Salesforcetutorial.com'.
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.
Example:-
Contact c =New Contact(); c.count of __c='4': Formula filed (This statement gives an error)
Accessing sObject fields through Relationships
Sobject records represent relationship to other words with two fields an ID and an address that points to a representation of the associated sobject.’
Example:- The contact sobject has both an AccountID field type ID and an Account field of type accounts 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 following Apex code shows how an account and contact can be associated with one other, and then how the contact can be used to modify a field on the Account.
Account a=new account (Name ='Osmania University') Insert a;
Inserting the record automatically assigns a value to its ID field.
Example:-
Contact c= new contact (Last name='Prasanth Kumar') C. Account ID=a.ID The new contact now points at the new account. Insert c;