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 as Burlington Textiles in apex will be referred using an 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 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’);

  1. 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’);

Our Recent Blog

Share This Post