An Introduction to Apex
According to the Apex Developer Guide, Apex is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Salesforce Platform server, in conjunction with calls to the API.
Apex is very similar to Java, such as variable declaration, expressions, conditions, loop syntax and array notation. It supports data manipulation statements such as insert, update, and delete operations. It also has a built-in support for key Salesforce features, including inline Salesforce Object Query Language (SOQL) and Salesforce Object Search Language (SOSL) queries that return lists of sObject records, looping that allows for bulk processing of multiple records at a time, locking syntax that prevents record update conflicts, and custom public API calls built from stored Apex methods.
Why Apex?
The following are some scenarios to consider on when we should use Apex:
- Create web services for complex integration.
- Create email services.
- Perform complex validation that spans multiple objects.
- Create complex business processes that aren’t supported by Flow Builder.
- Create custom transactional logic (logic that occurs over the entire transaction, not just with a single record or object).
- Attach custom logic to another operation, such as saving a record, so that it occurs whenever the operation is executed, regardless of whether it originates in the user interface, a Visualforce page, or from SOAP API.
Sign Up for a Developer Org
For someone who's just starting with learning Apex, I suggest that you sign-up for your own Developer Org on this link. This provides access to many of the features available, including the Developer Console where you can write, test, and debug your code directly within Salesforce. Although the Developer Console is a lightweight, built-in tool that’s ideal for beginners, you can also explore more advanced development environments like Visual Studio Code as you progress.
The Developer Console
The Developer Console is a built-in IDE (Integrated Development Environment) within Salesforce, where developers can complete most of the essential coding tasks. It functions similarly to Java IDEs like Eclipse or IntelliJ. Although there are other Development Environment options where you can write Apex code such as Visual Studio Code, the Developer Console is a powerful section that provides the following abilities.
- Add code on the source code editor, in addition to creating Apex classes.
- Save Apex class or trigger which could mean that the code is compiling.
- View debug logs and inspect the logs for locating performance bottlenecks.
- Query data using SOQL on the Query Editor
- Execute all or specific test classes and view test results
To open the Developer Console:
- From Lightning Experience: Click the gear icon on the top right, then click Developer Console
- From Salesforce Classic: Click Your Name, then Developer Console
Data Types
Before declaring variables in Apex, it must have a data type. The following are the common data types in Apex:
- A primitive data type, such as an Integer, Double, Long, Decimal, Date, Time, Datetime, String, ID, or Boolean.
- An sObject, which is either a generic sObject or a specific sObject (e.g. Account, Contact, MyCustomObject__c) NOTE: sObject stands for Salesforce Object.
- A collection, including:
- A list (or array) of primitives, sObjects, user defined objects, objects created from Apex classes, or collections
- A set of primitives
- A map from a primitive to a primitive, sObject, or collection
- Objects created from Apex classes
When to Use Lists, Maps, and Sets in Apex
- List: Use a list when you need an ordered collection of items. Lists are great for handling multiple records of the same type (e.g., a list of Account objects) and preserving the order of those records. Lists allow you to store and process data in bulk, making them essential when working with records retrieved from a SOQL query.
- Set: Use a set when you need a unique collection of items without duplicates, and the order doesn’t matter. Sets are helpful for filtering out duplicate values or performing bulk operations, such as adding a unique set of IDs to ensure you’re working with distinct records.
- Map: A map is a collection of key-value pairs, ideal when you need to associate each item with a unique key (e.g., ID to Account). Maps are highly efficient for lookups and retrievals, making them useful when you need to access data by a unique identifier, like matching an account name to its record ID.
Variables in Apex
Variable declaration in Apex is the same as Java. Multiple variables can be declared and initialized in a single statement, as shown below:
Integer count = 0;
String firstString, secondString, thirdString;
List<Account> accountList = new List<Account>();
Map<ID, String> idToStringMap;
Conditional Statements in Apex
String assessment = '';
if (score < 50) {
assessment = 'Needs Improvement';
assessment = 'Passed';
}
Loops in Apex
- do {statement} while (Boolean_condition);
- while (Boolean_condition) statement;
- for (initialization; Boolean_exit_condition; increment) statement;
- for (variable : array_or_set) statement;
- for (variable : [inline_soql_query]) statement;
Using for loop to iterate collection
for (Account acc : accountList) {
// process records here
}
Using SOQL for loop
for (Account acc : [SELECT Id, Name FROM Account]) {// process records here
- break; - exits the entire loop
- continue; - skips to the next iteration of the loop
Conclusion
Resources
Trailhead: Apex Basics & Database


0 Comments