ONE DAY SALE

Free Salesforce Developers Tutorials >

Chapter 3 - Apex Datatypes and Operators >

sObjects in Salesforce

sObjects in Salesforce

What You’ll Learn


S2 Labs

What Is sObjects in Salesforce?

Unlike any other programming language like Java or C#, Apex is tightly integrated with the database. Hence we do not have to create any database connection to access the records or insert new records. Instead, in Apex, we have sObjects which represent a record in Salesforce. 

For example, 

An account record named Burlington Textiles in Apex will be referred to using a 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 datatype

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();

Now there are 2 ways to assign field values to the contact sObject:

1. Through Constructor

Contact con = new Contact(firstName = ‘Shrey’, lastName = ‘Sharma’);

2. Using Dot Notation

Contact con = new Contact();
con.firstName = ‘Shrey’;
con.lastName = ‘Sharma’;

Similarly, for Custom Objects:

Student__c st = new Student__c(Name = ‘Arnold’);

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 Developer

Download Study Material

Get access to exclusive study material for Salesforce Certification and ace your exams!

Download Now

Our Salesforce Certification Courses

Hey there! Glad you made it through our Salesforce Developer Training for beginners . But wait! We've got some high-in-demand Salesforce courses for you to take your Salesforce skills to the next level, making you a desired professional in the Salesforce job market.

Post a Comment

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