Search Here

Apex sObject datatype

In Apex, the sObject data type is a fundamental and specialized data type used to represent 

standard and custom objects in Salesforce. Standard objects are built-in objects like Accounts,

 Contacts, Opportunities, and custom objects are objects you create to store data specific to your organization's 

needs. An sObject can represent both standard and custom objects.


The sObject data type is used to work with records of these objects in Salesforce. Here's an example of how to declare and use an sObject in Apex:


public class SObjectExample {

    public static void main() {

        // Declare an sObject variable for an Account

        Account acc = new Account();

        acc.Name = 'Sample Account';

        acc.Industry = 'Technology';


        // Insert the Account record

        insert acc;


        // Retrieve the inserted record by its ID

        Account retrievedAccount = [SELECT Id, Name, Industry FROM Account WHERE Id = :acc.Id LIMIT 1];


        System.debug('Account Name: ' + retrievedAccount.Name);

        System.debug('Industry: ' + retrievedAccount.Industry);


        // Update the Industry of the retrieved Account

        retrievedAccount.Industry = 'Finance';

        update retrievedAccount;


        System.debug('Updated Industry: ' + retrievedAccount.Industry);

    }

}



We declare an sObject variable acc to represent an Account object. We set some fields like Name and Industry.


We insert the acc record into the Salesforce database.


We retrieve the inserted record using a SOQL (Salesforce Object Query Language) query and assign it to retrievedAccount.


We use System.debug() to print the values of fields on the retrieved Account object.


We update the Industry field of the retrievedAccount and use the update statement to save the changes to the Salesforce database.


The sObject data type is essential for working with Salesforce data, querying records, creating new records, and updating existing records. It provides a flexible and generic way to work with various standard and custom objects within the Salesforce platform.




Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.