Apex is a programming language developed by Salesforce.
- It is an object oriented programming language that allows developers to execute flow and transaction control statements on the salesforce platform.
- It helps developers to add business logic to most of the system events like button clicks, record updates and Visual force pages.
- The syntax is similar to Java. Variable declaration, loop syntax, conditional statements are used in the same way as they are used in Java.
When to use Apex?
Apex should be used when it is not possible to implement a complex business functionality using the pre-built functionalities of Salesforce. Certain instances are given below:
- Web and email services
- Perform complex validation over multiple objects at the same time and also custom validation implementation.
- Create complex business processes that are not supported by existing automation tools like workflows or flows.
- Create custom transactional logic (logic that occurs over the entire transaction, not just with a single record or object) like using the Database methods for updating the records.
- Perform some logic when a record is modified or modify the related object’s record when there is some event which has caused the trigger to fire.
How Apex Works?
Apex is compiled, stored and run entirely on the Lightning Platform.
- Developer writes the Apex code and save it to the platform
- Application server compiles the code into a set of instructions that can be understood by the Apex Runtime interpreter and saves those instructions as metadata.
- End user triggers the Apex execution (by clicking a button or accessing a Visualforce page).
- Application server retrieves the compiled instructions from the metadata.

Programming elements of Apex
- Variables and Expressions
- Before using variables in the code, one must declare the data type of the variable.
- Basic datatypes include Integer, Date, and Boolean etc.
- Eg- Integer Count=1;
- Decimal Total;
- Advanced types are lists, sets, maps, objects and sObjects.
- List<String>var_list = new List<String>();
- List<String>var_list = new List<String>();
- Statements
- A statement is a coded instruction that performs an action.
- It can be,
- Assignment, such as assigning a value to a variable
- Conditional (if-else)
- Loops such as Do-while, While, For.
- A Do-while loop checks the condition after the code has executed.
- A While loop checks the condition at the start, before the code executes.
- A For loop enables you to more finely control the condition used with the loop.
- Data Manipulation Language (DML)
- Apex enables you to insert, update, delete or restore data in the database. DML operations allow you to modify records one at a time or in batches.
- Locking
- In Apex, keywords FOR UPDATE is used to lock sObject records while they’re being updated to prevent race conditions and other thread safety problems.
- While an sObject record is locked, no other or user is allowed to make updates either through code or the Salesforce user interface until the lock gets released when the transaction completes.
- Eg: Account [] accts = [SELECT Id FROM Account LIMIT 2 FOR UPDATE];
- Exception Handling
- Exceptions are special conditions that disrupt the normal flow of code execution.
- throw statements are used to generate exceptions, while try, catch, and finally statements are used to gracefully recover from exceptions.
- For learning more about the core concepts of Apex, please click Understanding Apex Core Concepts | Apex Developer Guide | Salesforce Developers
In this article we covered the basic concepts of Apex. In an upcoming article, we will discuss more on Apex Class, trigger and test.
Looking for more information on Apex, leave us a message.
Take Five Consulting is a technology company, based in Virginia U.S., that specializes in the Mortgage Banking vertical especially LOS implementation and application development. Take Five Consulting creates and implement mortgage technology and software specifically for Mortgage Industry.