sObjects in Salesforce
Chapter Topics
- Apex Data Types
- Integer Data Type in APEX
- Floating Point Data Type in APEX
- String Data Type in APEX
- Date Datatypes in APEX
- Time and DateTime Data Types in APEX
- Boolean Datatype in Salesforce
- ID & Blob Datatypes in APEX
- What is the Rule of Conversions in Apex?
- Enums in APEX
- sObjects in Salesforce
- Generic sObjects in Salesforce
- Collections in APEX
- Lists in APEX
- List Array Notation in APEX
- Lists Initialization in APEX
- Nested Lists in APEX
- Sets in APEX
- Sets Initialization And Methods
- Maps in APEX
- Map Initialization Methods in APEX
- Operators in Apex and Their Types
- Shorthand Operator
- Equality Operator
- Relational Operators(<,>,<=,>=)
What is sObjects in Salesforce?
In Salesforce sObjects represent a record. Unlike other programming languages like Java or C#, Apex is tightly integrated with the database. Hence, we do not have to create a database connection to access the records or insert new records. Instead, in Apex, we have sObjects.
sObjects Types in Salesforce
There are two main types of sObjects: Standard sObjects and Custom sObjects.
Standard sObjects
Salesforce provides preconfigured objects known as Standard sObjects. These constitute the fundamental business entities shared by the majority of enterprises, and they form the basis of the Salesforce platform. As examples, consider:
- Account: Stands in for a business, corporation, or other entity in a business arrangement. For instance, a rival company or business partner.
- Contact: This acts on behalf of people, such as leads, clients, partners, or staff members connected to a particular account.
- Lead: A ‘Lead’ indicates a possible client, someone with whom there may be future commercial contacts.
- Opportunity: Denotes a possible sale or business transaction.
Custom sObjects
When the regular sObjects are insufficient to meet specific business demands, custom sObjects are used. Salesforce customers have the ability to construct custom sObjects—sObjects made specifically for storing data that is tailored to their needs as a business.
It’s also crucial to remember that custom sObjects can be related to both other custom sObjects and regular sObjects. This makes it possible to build intricate data models that efficiently map your particular business operations.
Declaring sObject
For example, An account record named ‘Disney’ in Apex will be referred to using an sObject, like this:
Account acc = new Account(Name=’Disney’);
The API object Name becomes the data type of the sObject variable in Apex.
Here,
- Account: sObject data type
- 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();
Defining sObject :
There are 2 ways to assign field values to the contact sObject:
1. Through Constructor
Contact con = new Contact(firstName = ‘Shrey’, lastName = ‘Sharma’);
Similarly, for Custom Objects:
Student__c st = new Student__c(Name = ‘Arnold’);
2. Using Dot Notation
Contact con = new Contact();
con.firstName = ‘Shrey’;
con.lastName = ‘Sharma’;
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’);
Salesforce sObject Methods
The following are methods for SObject. All are instance methods.
S.No | Function | Example |
---|---|---|
1 | addError(errorMsg) Marks a trigger record with a custom error message and prevents any DML operation from occurring. |
Account acc = new Account(Name='Test Account'); |
2 | addError(errorMsg, escape) Marks a trigger record with a custom error message, specifies if the error message should be escaped, and prevents any DML operation from occurring. |
Account acc = new Account(Name='Test Account'); |
3 | addError(exceptionError) Marks a trigger record with a custom error message and prevents any DML operation from occurring. |
Account acc = new Account(Name='Test Account'); |
4 | addError(exceptionError, escape) Marks a trigger record with a custom exception error message, specifies whether or not the exception error message should be escaped, and prevents any DML operation from occurring. |
Account acc = new Account(Name='Test Account'); |
5 | addError(fieldName, errorMsg) Dynamically add errors to fields of an SObject associated with the specified field name. |
Account acc = new Account(Name='Test Account'); |
6 | addError(fieldToken, errorMsg) Dynamically add errors to an SObject instance associated with the specified field. |
Account acc = new Account(Name='Test Account'); |
7 | addError(fieldName, errorMsg, escape) Dynamically add errors to fields of an SObject associated with the specified field name. |
Account acc = new Account(Name='Test Account'); |
8 | addError(fieldToken, errorMsg, escape) Dynamically add errors to an SObject instance associated with the specified field. |
Account acc = new Account(Name='Test Account'); |
9 | clear() Clears all field values |
Account acc = new Account(Name='Test Account', Industry='Technology'); |
10 | clone(preserveId, isDeepClone, preserveReadonlyTimestamps, preserveAutonumber) Creates a copy of the SObject record. |
Account acc = new Account(Name='Test Account'); |
11 | get(fieldName) Returns the value for the field specified by fieldName, such as AccountNumber. |
Account acc = new Account(Name='Test Account'); |
12 | get(field) Returns the value for the field specified by the field token Schema.sObjectField, such as Schema.Account.AccountNumber. |
Account acc = new Account(Name='Test Account'); |
13 | getCloneSourceId() Returns the ID of the entity from which an object was cloned. |
Account acc = new Account(Name='Original Account'); |
14 | getErrors() Returns a list of Database.Error objects for an SObject instance. |
Account acc = new Account(); |
15 | getOptions() Returns the database.DMLOptions object for the SObject. |
Account acc = new Account(); |
16 | getPopulatedFieldsAsMap() Returns a map of populated field names and their corresponding values. |
Account acc = new Account(Name='Test Account', Industry='Technology'); |
17 | getSObject(fieldName) Returns the value for the specified field. Used primarily with dynamic DML to access external IDs. |
Account acc = [SELECT Id, (SELECT Id FROM Contacts) FROM Account WHERE Name='Test Account' LIMIT 1]; |
18 | getSObject(field) Returns the value for the field specified by the field token Schema.sObjectField. |
Account acc = [SELECT Id, (SELECT Id FROM Contacts) FROM Account WHERE Name='Test Account' LIMIT 1]; |
19 | getSObjects(fieldName) Returns the values for the specified field. Primarily used with dynamic DML to access child relationships. |
Account acc = [SELECT Id, (SELECT Id FROM Contacts) FROM Account WHERE Name='Test Account' LIMIT 1]; |
20 | getSObjects(field) Returns the value for the field specified by the field token Schema.fieldName. |
Account acc = [SELECT Id, (SELECT Id FROM Contacts) FROM Account WHERE Name='Test Account' LIMIT 1]; |
21 | getSObjectType() Returns the token for this SObject. |
Account acc = new Account(); |
22 | getQuickActionName() Retrieves the name of a quick action associated with this SObject. |
Account acc = new Account(); |
23 | hasErrors() Returns true if an SObject instance has associated errors. |
Account acc = new Account(); |
24 | isClone() Returns true if an entity is cloned from something. |
Account acc = new Account(Name='Test Account'); |
25 | isSet(fieldName) Returns true if the sObject field is populated, either by direct assignment or by inclusion in a SOQL query. |
Account acc = new Account(Name='Test Account'); |
26 | isSet(field) Returns true if the sObject field is populated, either by direct assignment or by inclusion in a SOQL query. |
Account acc = new Account(Name='Test Account'); |
27 | put(fieldName, value) Sets the value for the specified field and returns the previous value for the field. |
Account acc = new Account(Name='Test Account'); |
28 | put(field, value) Sets the value for the field specified by the field token Schema.sObjectField. |
Account acc = new Account(Name='Test Account'); |
29 | putSObject(fieldName, value) Sets the value for the specified field. Primarily used with dynamic DML for setting external IDs. |
Account acc = new Account(name='Acme', description='Acme Account'); |
30 | putSObject(fieldToken, value) Sets the value for the field specified by the token Schema.SObjectType. |
Account acc = new Account(Name='Test Account'); |

Join our newsletter: Get daily update on Salesforce career insights & news!
Join Now!
Need more support?
Get a head start with our FREE study notes!
Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.