1.1 What is SObject and how do we use it in Apex

Before we start

Before we proceed, let’s create an SObject according to the following image in your org:


The available values for Discount Type picklist are: Normal price, Good price, Half price.

What is an SObject

In short, an SObject is an object in Salesforce’ perspective. It can be a custom object as well as a standard object. For example, Merchandise__c is an SObject type. Please be aware that custom object and custom object’s API always ends with __c which is double underscore and c.

An SObject can be pretty much used in the same way as a class in Apex. Here is the code:

Merchandise__c mer = new Merchandise__c();
mer.Original_Price__c = 100;
mer.Discount_Type__c = 'Half price';
mer.Discounted_Price__c = mer.Original_Price__c * 0.5;

System.debug('Discounted Price is: ' + mer.Discounted_Price__c);

Run and execute this code in anonymous block.

A bit explanation

SObject is first class citizen in Apex. You can use them almost in the same way as normal classes.

All the number fields are default to be Decimal in Apex, and the text and picklist fields are default to be String. Checkbox is Boolean.


Create an Account record in Apex. And try using it.

Next Post

1.2 What is Salesforce Id and why do we have two sets of Ids in Salesforce

Subscribe to Sfdcinpractice

Subscribe to get the latest blogs and tutorials of sfdcinpractice. No spam, no trash, only the awesome posts from sfdcinpractice. 

Leave a Reply

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