Salesforce Apex Tutorial – Chapter 1: SFDC Developer course for beginners

Salesforce Apex Tutorial – Chapter 1: SFDC Developer course for beginners

On September 24, 2021, Posted by , In Salesforce Apex Tutorial, With Comments Off on Salesforce Apex Tutorial – Chapter 1: SFDC Developer course for beginners

What is Salesforce Apex? [Learn apex programming for beginners]

The Apex programming language was developed by Salesforce.com. It is a strongly typed, object-oriented language that allows developers to call the API of Force.com in conjunction with flow and transaction control statements on the Force.com platform server. The syntax of Apex is similar to that of Java. Apex can be executed on click of a button, weblinks, triggers, and visual force pages. Apex is available in several Salesforce editions like Enterprise, Performance, Unlimited, Developer, and Database.com editions.

You can find the Salesforce Apex Tutorial Chapter 2: Apex Environment as next chapter, sequel to this one.

Salesforce Apex Tutorial - Chapter 1: what is Apex
Salesforce Apex Tutorial – Chapter 1: what is Apex

Features of Apex – Salesforce programming for beginners :

Salesforce Apex is loaded with several awesome features. Here’s a list of some important features:

  • The Apex programming language is strongly typed. If a schema object, like sObject, is deleted or is the wrong type, a reference to that object fails immediately.
  • Apex is a case-insensitive programming language.
  • Apex supports DML operations like Insert,Update,Delete etc.
  • Apex can execute multiple queries and DML statements concurrently.
  • Apex runs in a multitenant environment, and salesforce has set up governor limits for each user, so a user can not control shared resources. Any code that violates governor limits fails with an error message.
  • Salesforce automatically updates Apex with every new release.
  • In Apex, code coverage and efficiency of code can be checked by creating and executing Unit test cases that indicate which part of your code needs to be improved.

When Should Developer Choose Apex programming as beginner?:

Apex Code should only be written if a business scenario is too complex and can’t be implemented using the pre-built functionality provided by salesforce. Following are a few use cases where apex can be used over salesforce configuration:

  • To create web services that integrate salesforce with other applications.
  • To implement custom and complex validations on Sobjects.
  • To implement complex business processes that can’t be achieved using the out of the box salesforce functionality( Workflow, process builder, and flows)
  • To perform apex logic when a DML operation happens in Salesforce Database.
  • To set up email services which include processing the contents, headers and attachments of the email using the apex code.

Structure of Apex:

Following are the actions that happen when a developer saves the code and an end-user performs some action that invokes Apex code.

Developer Action: Whenever a developer builds and saves the apex code, it compiles the code into a set of instructions that the Apex runtime interpreter can read and save that information as metadata.

End-User Action: As soon as a user event executes apex code, the server receives the compiled instructions from the metadata and sends them through the apex runtime interpreter to the platform server before returning the result.

Below is the architecture of Apex programming and how it runs on the Force.com platform.

Salesforce Apex Tutorial - Chapter 1: Apex Overview Structure of Apex
Salesforce Apex Tutorial – Chapter 1: Apex Overview Structure of Apex

Syntax of Apex – Salesforce programming tutorial syntax: 

Apex Code contains many elements which are similar to other programming languages. Following are a few elements of the apex language.

  • Variable Declaration: Since Apex is a strongly typed language, every variable must have its own data type.
For example, Integer XYZ = 2; and String ABC = ‘test’;Here, XYZ is a variable of datatype Integer, ABC is a variable of datatype string.
  • SOQL Query: SOQL(salesforce object query language) directly interacts with the salesforce database and is used to fetch data from the salesforce database.

The below query is used to fetch Contact object records from the Salesforce database.

Contact[] con = [Select Id, FirstName, LastName from Contact];
  • Loop Statements: For iterating over a list of records, or a piece of code for a given number of times, Loop statements are used.

Below is the sample code used to iterate over the list of the Contact object. 

List<Contact> listOfContact = new List<Contact>();// iteration over the contact listFor(Contact con:listOfContact ){//logic};
  • Flow Control Statement:  Flow control statements are used when a user wants to execute or stop the execution of a piece of code on specific conditions.

Below code execute the logic if the size of the contact list is greater than zero

List<Contact> conList = [Select Id,FirstName, LastName from Contact Limit 10];If(conList .size()> 0){// execute the logic if the size of the contact list is greater than zero};
  • DML Statements: Data manipulation language(DML) is used to perform  Insert, update, upsert, and delete operations over records in the Salesforce database.

The below code is used to delete the account record of having the name ‘Test’.

List<Contact> conList = [Select Id,FirstName, LastName from Contact Where LastName = ‘Test ’];Delete conList ;

Following is the sample code snippet

public class ApexDemoController{ 
Public static void updateContact(){ 
// variable declaration
List<Contact> conList = new List<Contact>();
// SOQL query 
conList = [Select Id,FirstName,LastName from Contact Where LastName = 'Test'];
List<contact> listToUpdate = new List<Contact>();
// iteration over contact list
for(contact con:conList){
con.Title = 'Manager';
listToUpdate.add(con);
}
// flow control statement
if(listToUpdate != null || listToUpdate.size()> 0 ){
// DML statement
update listToUpdate;
}    
}
}

Learn Salesforce Apex programming tutorial for beginners step-by-step chapter wise approach from CRS info solutions.

Comments are closed.