Working with sObject

What is sObject?

SObject refers to any object that can stored in Lightning Platform Database. In Salesforce, objects function similarly to tables in a database, with fields representing columns and records representing rows within these objects. In Apex, the sObject data type holds data objects. It is a term used to represent the standard and custom object from salesforce database in apex.

As we know, Apex is tightly integrated with database hence, we don’t need to establish connection with database explicitely. SObject variable represents a row of data from database. Syntax for declaring sObject (not generic one) ObjectAPIName nameOfVariable; We can do declaration and initialization in single line ObjectAPIName nameOfVariable = new ObjectAPIName();

  1. ObjectAPIName : sObject (non-primitive) data type may be standard or custom object from salesforce.
  2. nameOfVariable : Name of Variable.
  3. new : It creates the instance of class and allocate memory to it.
  4. () : brackets are for constructor.
Account a; //declaration 
System.debug(a);

Account acc = new Account();//declaration + Initialization
System.debug(acc);

//Assign field values using constructor
Account accnt = new Account(Name = 'Sam', AccountSource = 'Web', Industry = 'Energy');
System.debug(accnt);

//Assign field values using dot notation
Account acco = new Account();
acco.Name = 'Tom';
acco.AccountSource = 'Web';
acco.Industry = 'Energy';
System.debug(acco);
USER_DEBUG [2]|DEBUG|null
USER_DEBUG [5]|DEBUG|Account:{}
USER_DEBUG [8]|DEBUG|Account:{Name=Sam, AccountSource=Web, Industry=Energy}
USER_DEBUG [14]|DEBUG|Account:{Name=Tom, AccountSource=Web, Industry=Energy}

Generic sObject

In Apex, we can declare sObject variable without specifying the object type at compile time. It is a superclass of all custom and standard objects. sObject allows you to work with any object dynamically. It provides flexibility when writing code that needs to handle various types of objects.

SObject obj = new SObject(); //Not allowed Exception: Type cannot be constructed: SObject 

List<SObject> objList = new List<SObject>();
System.debug(objList);
USER_DEBUG [2]|DEBUG|()

get(fieldAPIName) and put(fieldAPIName,value) method

Dot notation is not allowed for accessing fields on generic SObject variables because, compiler cannot determine the fields available on SObject. Therefore, we can use get(fieldAPIName) to read field value from generic SObject variable but, return type of get method is Object hence, it must be typecasted before use. put(fieldAPIName,value) is use for assigning value to specific field.

SObject acc = new Account(Name = 'Cilian', AccountSource='Web');
System.debug(acc);
acc.put('Industry','Agriculture');
System.debug(acc);
String str = String.valueOf(acc.get('Name'));
System.debug(str);
USER_DEBUG [2]|DEBUG|Account:{Name=Cilian, AccountSource=Web}
USER_DEBUG [4]|DEBUG|Account:{Name=Cilian, AccountSource=Web, Industry=Agriculture}
USER_DEBUG [6]|DEBUG|Cilian

How to access related records from generic SObjects?

//Below code is for demo purpose
//Please don't hardcode Id in code. It's not a best practice
SObject opp = [SELECT Id, Name, Account.Name FROM Opportunity WHERE Id='0062w00000JzF1MAAV'];
System.debug(opp);
System.debug(opp.getSObject('Account').get('Name'));
USER_DEBUG [4]|DEBUG|Opportunity:{Id=0062w00000JzF1MAAV, Name=Dickenson Mobile Generators, AccountId=0012w00001LOsRgAAL}
USER_DEBUG [5]|DEBUG|Dickenson plc

Difference between Object and SObject data type

An Object is any type of data that can be represented. All apex data types even sObjects inherit from Object. We can typecast object to more specific data type. It is typically used when we want to work with arbitrary data which may not tied to Salesforce platform.

sObjects are specific to salesforce platform and represents any standard or custom objects (tables) from salesforce database.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *