I created a custom logging feature to track changes to records in any object, such as an account, contact, or custom object. The method should accept the SObject type to work with any object in Salesforce. For that, we need to declare a SObject in Salesforce Apex.
Here, I will explain what an SObject is in Salesforce Apex and why it is necessary to use it. Next, I will explain how to declare a SObject in Salesforce Apex, including its methods and their explanations.
SObject in Salesforce Apex
In Salesforce, sObjects (Salesforce Object) represent a record. Unlike other programming languages, such as 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.
It allows for dynamic handling of records when the object type is unknown at compile time. Key methods, such as get(), set(), and getPopulatedFieldsAsMap(), enable flexible access and modification of field values.
SObject can be cast to specific object types when needed. It is commonly used in triggers, utility methods, and bulk data processing, making it essential for handling records dynamically and reusable.
Types of SObjects in Salesforce
- Standard SObjects: Salesforce provides preconfigured objects known as Standard SObjects. These constitute the fundamental business entities shared by most enterprises and form the basis of the Salesforce platform.
- Custom SObjects: When the regular SObjects are insufficient to meet specific business demands, custom SObjects are used. Salesforce customers can construct custom sObjects—sObjects explicitly made for storing data tailored to their business needs.
It’s also crucial to remember that custom sObjects can be related to other custom sObjects and regular sObjects. This enables the creation of intricate data models that efficiently map your business operations.
1. Declare SObject Through Constructor
Syntax to declare SObject in Salesforce Apex:
For example, we have an account record named ‘TSinto Technologies’ that we want to refer to in Apex. To do so, we need to access it, as shown below.
Account acc = new Account ( Name = ’TSinto Technologies’ );
- Account: This is a SObject data type.
- acc: is the variable that we declared.
- Name: This is the API Name that we saved while creating the fields.
Similarly, if a Custom SObject is named Student, the syntax is below.
Student__c stud= new Student__c (Student_Name__c = ’Alex Anderson’ );
Here, we need to give the suffix as a double underscore c (__c), which denotes a custom object. The same is true for fields.
2. Declare SObject Using Dot Notation
Contact con = new Contact();
con.FirstName = 'Alex Anderson';
Here, first, we declare SObject, and then, using instance and dot notation, we can assign values to the fields.
Methods Used in Salesforce SObject
Below, I explained some of the methods we use in Apex Salesforce SObject.
1. get(FieldName) method to Fetch Records From SObject
In the apex code below, I declare