Salesforce: Introduction to APEX Programming Language

Apex is an object-oriented programming language developed by As we know Salesforce is one of the best CRM platforms to provide out-of-box functionalities, such as process builder, workflow, etc., to achieve the business requirements. But to get some advanced functionalities or build custom applications, these predefined functions are not sufficient to get the work done, and for such cases, we need to write the code. These codes can be written with the help of Apex programming language.

So, we can say, “Apex is the programming language used by the Salesforce developers to build the next-generation business applications.”

  • The apex has a syntax that looks similar to the Java language and works as a database stored procedure.
  • It is strongly typed language, here the term strongly types means if we are using any variable or constant, we need to define their data types also.
  • With the help of Apex programming language, we can add business logic to system events, button clicks, record updates, etc. It means if we want to open a new URL by clicking on the button in Salesforce, that we can add logic using Apex.
  • We can initiate the Apex code using Web Service requests and triggers on objects.

Features of Apex Programming Language

The main features of the Apex programming language are given below:

  • Integrated

It has inbuilt support for DML operations; hence we can easily Update, Insert, or Delete the records using APEX. It also provides direct access to record and their fields, and enable us to manipulate those records using the query language.

  • Strongly-typed

It is strongly typed language; hence it validates the references of objects at compile time. Any invalid reference immediately fails if it is removed or of the wrong data type.

  • Java-like Syntax

It has Java-like Syntax, so if someone has experience in Java programming language, then he can easily learn it.

  • Upgrades Automatically

Automatic upgrades are available, so we don’t need to be bothered about manual upgrades.

  • Easy Testing

It allows us to easily test the program using the testing framework, which allows us to write unit tests, check test results, and have code coverage results. With test results, it tells us how much code is covered, and which part of the code could be more efficient.

  • Object-oriented

It is an object-oriented language hence it supports classes, interface, and inheritance, similar to other object-oriented programming languages.

  • Multitenant aware

Apex runs in the multitenant platform; hence the Apex runtime engine is designed in a way to prevent the code from monopolizing the shared resources.

  • Versioned

It is versioned, which means we can save the custom apex code with different versions of API.

  • Available Editions

Apex is available in Performance Edition, Unlimited Edition, Developer Edition, Enterprise Edition, and

  • Case-Sensitive

Apex is a Case-sensitive language.

  • Hosted

It is interpreted, executed, and controlled by the Lightning Platform.

Apex Programming: Development Tools

To write the Apex code, we need development tools. We can write the code either in sandbox or development environments. Salesforce provides mainly three development environments, which are:

  • Developer Console

The developer console is the integrated development environment that contains various tools, and with these tools, we can create debug, and test applications in our Salesforce Org.

  • Salesforce Extensions for Visual Studio Code

The Salesforce Extensions for VS Code also provides us various tools that enable us to write the Apex code on the Client.

  • Code Editor in Salesforce UI

We can write Apex and access debugging information directly in the browser by using the Salesforce user interface.

How does Apex Programming Language Work?

The complete Apex programming is done on the Salesforce Lightning-platform. Developers write, save, and debug the apex program to the platform. As per the requirement, the end-user triggers the execution of the program using the user interface.

The below diagram illustrates the working of Apex code:

Apex programming language by Salesforce
Apex programming language by Salesforce

From the above image, when the developer user writes & saves the code, the application server first compiles the code and sends the compilation errors(if any) back to the developer. The server saves the code as metadata after compilation.

When the end-user triggers the execution of Apex such as by clicking on a button, or accessing a visualforce page, at that instant application server retrieves the compiled instruction from the metadata. These instructions are sent to the end-user through a runtime interpreter, but the user cannot observe any difference in execution time.

Apex Syntax| Core Concepts of Apex

Each language has some syntax, and the syntax is the rules which allow the developer to write efficient code. As we have discussed Apex has Java-like syntax, so it contains various things that are also used in Java. A typical Apex syntax contains the following programming elements:

  • Variable Declaration

The variables are used to store the data, and declaring a variable with its datatype within a program is known as a variable declaration. It includes basic data types such as Integer, Boolean, Date, etc., and advanced types such as lists, maps, objects, and sObjects. The following syntax is used for the variable declaration:

datatype​ variable_name [ = value];


Integer num = 20;

In the above example, Integer is the data type, num is variable, 20 is the assigned value. We can assign this value later also.

Note: To end a statement in Apex, the use of a semicolon (;) is a must.

  • Loop Statement

The loop statements are used when we want to iterate over a given list of records or to do the same thing again and again based on the given condition. Using loops, we can make our code faster and simpler. Apex supports three types of Looping Statements:

    • Do-while
    • While
    • For
  • Flow Control Statement

If we want to apply a filter or to perform a task by applying a condition, then we can write the logic within the Flow control statement. It is also called branching which allows our application to do different tasks based on the given condition. For flow control, we use if-else statements.

The basic syntax for

if (true)




  • DML Statement

The DML statements are used to update, insert, or delete the records

  • SOQL Query

The SOQL query is used to retrieve the data from Salesforce. It helps to get the data from a particular account.

  • Collections

Apex has mainly three types of collection, which are:

    • Lists(array): It is the collection of elements such as Integer, String, Objects, or other collection. The list is created using the keyword new. The List keyword is used followed by the type of the elements contained within <>. The following syntax is used to create a list.

List <datatype> list_name

[= new List<datatype>();] |

[=new List<datatype>{value [, value2. . .]};] |

; Example:

List<Integer>First _List = new List<Integer>();

  • Maps

A map is a group of key-value pairs, where the key can be any primitive data type, and a value can include a primitive data type, as well as other collections and objects. In the map, the key should be unique, but the value may be duplicated.

To create the map, we can use a new keyword, and Map keyword followed by the key-value pair enclosed within <>.

The following syntax can be used to create a Map:

Map<key_datatype, value_datatype> map_name

[=new map<key_datatype, value_datatype>();] |

[=new map<key_datatype, value_datatype>

{key1_value => value1_value

[, key2_value => value2_value. . .]};] |


  • Sets: It is a collection of unique and unordered elements. It can contain primitive data types, such as Integer, String, etc., and complex data types such as sObjects.

We can create the set using a new keyword, and the Set keyword followed by the data type contained within <> characters.

The syntax to create a set is:

Set<datatype> set_name

[= new Set<datatype>();] |

[= new Set<datatype>{value [, value2. . .] };] |

; Example:

Set<String> First_String = new Set<String>{‘1’, ‘2’, ‘3’};

When Developers should choose Apex?

One of the best qualities of a Salesforce developer is that he must be aware of when there is a requirement to write the code, and when it is not. As we all know Salesforce has existing out-of-box functionalities to achieve any business requirement. But if these functionalities are not enough to do the required task, then only developers should choose the Apex to write the logic or fulfill a complex requirement.

Use the apex to fulfill the below requirements:

  • To create the web services
  • TO create email services
  • To add custom logic to another operation, such as Saving a record.
  • To perform complex validation for multiple objects.
  • To create custom transactional logic.
  • To create complex business processes that cannot be created using workflows.

About the Author: Admin

You May Also Like

Leave a Reply

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

Join Our Community Of Bloggers