Salesforce Apex Tutorials | Learn Apex Programming Language
Salesforce Apex is the programming language used to write custom server-side logic on the Salesforce Platform. It is strongly typed, object-oriented, and uses Java-like syntax, so developers who already know Java or C# can understand the basic structure quickly. Apex is used when declarative tools are not enough to meet a business requirement.
In practical Salesforce development, Apex helps you add transaction control, work with records, call business services, build custom controllers, and run logic when users create, update, or delete data. Before writing code, it is useful to understand Salesforce objects, records, relationships, security, and the difference between declarative configuration and programmatic development.
Salesforce Apex is often used with Visualforce, Lightning Web Components, integrations, asynchronous jobs, and database operations. In older Visualforce applications, Apex controllers are part of the MVC architecture. In modern Lightning applications, Apex is commonly used to expose server-side methods to components and to run secure backend logic.
This Salesforce Apex tutorial is written for beginners who want a clear learning path before moving to triggers, SOQL, DML, test classes, governor limits, and deployment. If you are starting as a Salesforce developer, learn the platform basics first, then use Apex for the parts that require code.
What is Salesforce Apex programming language?
Salesforce Apex programming language is a proprietary language that runs on Salesforce servers. The official Salesforce Apex Developer Guide describes Apex as an object-oriented language for executing flow and transaction control statements on the Salesforce Platform. The key point for beginners is simple: Apex lets you write custom business logic close to Salesforce data.
Apex is not a general desktop or mobile programming language. It is designed for Salesforce applications and works directly with Salesforce records, permissions, transactions, and platform events. Apex code is saved as metadata in Salesforce, compiled by the platform, and executed in a controlled multitenant runtime.
- Apex classes contain reusable logic, methods, variables, and service code.
- Apex triggers run automatically before or after database events such as insert, update, delete, and undelete.
- Apex test classes verify logic and are required for production deployment.
- Apex works with SOQL, SOSL, and DML to query, search, create, update, and delete Salesforce records.
- Apex follows governor limits because Salesforce runs many customers on shared infrastructure.
Earlier Salesforce training material, including this Apex Programming language overview, often explains Apex as the code layer of the Force.com platform. The current wording used by Salesforce is Salesforce Platform or Lightning Platform, but the basic idea remains the same: use Apex when custom backend logic is needed.
Salesforce Apex tutorial prerequisites for beginners
You do not need to master every Salesforce feature before learning Apex. However, Apex code becomes easier when you already understand how records, fields, and relationships work in a Salesforce org. You should also know how to create a developer org or sandbox organization for practice.
- Salesforce object model: Understand standard objects such as Account, Contact, Lead, and Opportunity, along with custom objects and fields.
- Basic programming: Know variables, data types, if-else conditions, loops, methods, and classes. Familiarity with Programming tools is helpful.
- SOQL basics: Learn how Salesforce Object Query Language retrieves records from Salesforce.
- DML basics: Learn how Apex inserts, updates, upserts, deletes, and undeletes records.
- Testing mindset: Apex is deployed with unit tests, so you should write testable logic from the beginning.
Salesforce Apex programming language features used in real projects
Apex is designed for business applications where data integrity, transactions, and platform limits matter. The most important Apex features are not just syntax features; they are features that help code work safely inside Salesforce.
- Strong typing: Variables and method parameters have declared data types, which helps catch errors early.
- Object-oriented structure: Classes, interfaces, inheritance, and methods help organize business logic.
- Database integration: SOQL, SOSL, and DML are built into the language for Salesforce data access.
- Transaction handling: Apex operations usually run inside a transaction, so related work can succeed or fail together.
- Exception handling: try-catch-finally blocks help handle errors in database operations, integrations, and custom logic.
- Record locking support: Apex can work with Salesforce record locking behavior to reduce conflicting updates.
- Unit testing: Test classes and assertions are part of Apex development and deployment.
- Asynchronous processing: Future methods, Queueable Apex, Batch Apex, and Scheduled Apex help process work outside the current request.
- Web service support: Apex can expose or call services when an integration needs server-side Salesforce logic.
- Metadata storage: Apex classes and triggers are stored as metadata and can be moved between environments.
When should a Salesforce developer use Apex instead of Flow?
Salesforce provides declarative tools such as Flow, validation rules, approval processes, and formula fields. Use those tools first when they can solve the requirement clearly. Use Apex when the requirement needs code-level control, complex branching, advanced data processing, reusable services, or behavior that cannot be maintained well in declarative automation.
| Requirement | Better starting point | Why |
|---|---|---|
| Simple field update when a record changes | Flow | Usually easier to build and maintain without code. |
| Complex transaction across many related records | Apex | Code can control queries, DML, errors, and reusable logic more precisely. |
| Basic validation on one record | Validation rule | Declarative validation is easier to audit. |
| Custom logic called by Lightning Web Components | Apex | Server-side methods can retrieve and process Salesforce data securely. |
| Large data processing job | Batch Apex or Queueable Apex | Asynchronous Apex is designed for work that should not run in a single user request. |
| Scheduled backend action | Scheduled Flow or Scheduled Apex | Choose Apex when the schedule needs code, complex queries, or reusable services. |
How Salesforce Apex works on the Salesforce Platform

The working structure of Apex can be understood in two parts: what happens when a developer saves code and what happens when a user action runs that code.
- Developer writes Apex code: The developer creates a class, trigger, or test class in Developer Console, Salesforce Extensions for VS Code, or another supported development tool.
- Salesforce compiles the code: When the code is saved, Salesforce checks the syntax and compiles valid code.
- Code is stored as metadata: Apex is stored in the org as metadata, so it can be retrieved, deployed, version controlled, and packaged.
- User or system action runs the logic: Apex can run because of a record save, button click, component call, scheduled job, API operation, or platform event.
- Salesforce enforces limits and security: The code runs within platform limits and must respect the security design chosen by the developer.
Apex programming language runs entirely on the Salesforce Platform. The end user does not install Apex code locally. The user interacts with Salesforce through the user interface, API, automation, or a component, and Salesforce executes the required Apex logic on the server.
Basic Apex code structure: class, method, trigger and SOQL
Apex code is normally organized into classes. A class can contain methods, and a method can run logic such as queries, calculations, validations, or DML operations. Triggers should usually stay small and delegate logic to classes, because classes are easier to test and reuse.
Apex class syntax example for a beginner
public class AccountNameFormatter {
public static String formatName(String accountName) {
if (String.isBlank(accountName)) {
return 'Unnamed Account';
}
return accountName.trim();
}
}
This simple Apex class has one public static method. It accepts a String value, checks whether it is blank, and returns a cleaned-up value. The syntax looks similar to Java, but the code runs in Salesforce and can use Salesforce-specific classes and database operations.
SOQL query syntax inside Salesforce Apex
SELECT Id, Name, Industry
FROM Account
WHERE Industry = 'Technology'
LIMIT 10
SOQL is used to query Salesforce records. It looks similar to SQL, but it is designed for Salesforce objects and relationships. In Apex, SOQL can be written inside square brackets and assigned to a list of sObjects.
List<Account> technologyAccounts = [
SELECT Id, Name, Industry
FROM Account
WHERE Industry = 'Technology'
LIMIT 10
];
Apex trigger example for an Account record change
trigger AccountBeforeInsert on Account (before insert) {
for (Account acc : Trigger.new) {
if (String.isBlank(acc.Name)) {
acc.Name = 'Unnamed Account';
}
}
}
This trigger runs before Account records are inserted. It uses Trigger.new to access records in the current transaction. In real projects, avoid putting large business logic directly in triggers. Use a trigger handler class so the logic is easier to test and maintain.
How to write Apex code in Salesforce Developer Console and VS Code
Apex programming code can be written in more than one development environment. Beginners can start with Force.com developer console, while project teams commonly use Visual Studio Code with Salesforce Extensions and Salesforce CLI for source-driven development.
Why Salesforce Developer Console is useful for Apex beginners
Developer Console in Salesforce is an integrated browser-based development environment. It provides tools to create code, inspect logs, run anonymous Apex, execute tests, and query data. Using Force.com Developer Console, you can perform the following tasks.
- Create and edit Apex classes, triggers, Visualforce pages, and related resources.
- Save code and see compilation errors when syntax or references are invalid.
- Run anonymous Apex for quick testing and learning.
- View debug logs and inspect execution details.
- Run Apex tests and check code coverage.
- Query Salesforce data while learning SOQL and SOSL.
How to open Developer Console in Salesforce
To open Developer Console in Salesforce, click the gear icon or your user menu in the Salesforce header, and then select Developer Console. The exact menu position can vary by Salesforce experience and org setup.

Understanding Salesforce Developer Console interface for Apex coding
Developer Console in Salesforce includes tools for coding, debugging, testing, and inspecting Salesforce applications.

- Menu Bar: The menu bar includes options such as File, Edit, Debug, Test, Workspace, and Help. These menus help you create Apex triggers and classes, edit code files, run tests, and review logs.
- Workspace: The workspace area shows open resources in tabs. You can switch workspaces, create a new workspace, rename the current workspace, and manage open items.
- Logs, Tests, and Problems panel: This panel helps you review debug logs, test results, checkpoints, execution details, and code coverage issues.
Using Salesforce CLI and VS Code for Apex projects
For a team project, use source control and a local development setup. Salesforce Extensions for Visual Studio Code and Salesforce CLI help retrieve metadata, deploy Apex, run tests, and manage scratch orgs or sandboxes. Developer Console is still useful for quick inspection, but VS Code is better for larger codebases.
sf project deploy start --source-dir force-app
sf apex run test --test-level RunLocalTests
The commands above are examples of the type of workflow a Salesforce developer may use from a terminal. Always confirm the command options for your installed Salesforce CLI version before using them in a live project.
Salesforce Apex testing, deployment and 75% code coverage
Apex testing is not optional for production development. Salesforce requires Apex tests for deployment, and the commonly cited minimum is at least 75% Apex code coverage with passing tests. Every trigger must also have test coverage. Treat 75% as a deployment threshold, not as the definition of good testing.
- Write tests for successful paths, error paths, bulk data, and permission-sensitive behavior.
- Use test data created inside the test class instead of relying on existing org data.
- Use assertions to verify expected results rather than only executing code.
- Test trigger logic with multiple records so governor-limit issues are easier to find.
- Run tests before deploying Apex to production or packaging code.
Apex test method example with an assertion
@IsTest
private class AccountNameFormatterTest {
@IsTest
static void returnsDefaultNameWhenBlank() {
String result = AccountNameFormatter.formatName('');
System.assertEquals('Unnamed Account', result);
}
}
This test checks one expected behavior of the sample class. A useful Apex test should prove that the code produces the correct result, not just increase coverage numbers.
Salesforce Apex governor limits beginners should understand
Governor limits are runtime limits enforced by Salesforce to keep code efficient and fair in a multitenant environment. A beginner does not need to memorize every limit on day one, but every Apex developer must learn the patterns that avoid limit errors.
- Avoid SOQL inside loops: Query records once, store them in a collection, and process them in memory.
- Avoid DML inside loops: Build a list of records and perform one DML operation where possible.
- Bulkify triggers: Triggers can receive many records at once, so code must handle lists, not only one record.
- Use maps and sets: Collections help match related records without repeated queries.
- Choose async Apex carefully: Queueable, Batch, Future, and Scheduled Apex solve different problems.
Bulk-safe Apex pattern for records in a trigger
Set<Id> accountIds = new Set<Id>();
for (Contact con : Trigger.new) {
if (con.AccountId != null) {
accountIds.add(con.AccountId);
}
}
Map<Id, Account> accountsById = new Map<Id, Account>([
SELECT Id, Name
FROM Account
WHERE Id IN :accountIds
]);
This pattern collects Account Id values first and then runs one SOQL query. It is safer than querying inside the loop for every Contact record.
Salesforce Apex programming language course topics
These Salesforce Apex tutorials are targeted for a Salesforce Developer who wants to learn Salesforce Apex programming language step by step. If you have used Java Programming language before, Apex syntax will feel familiar, but Salesforce data access, governor limits, testing, and deployment rules must be learned separately from Java.
- What is Apex programming language?
- When should we use Apex in Salesforce?
- How to write Apex code in Developer Console and VS Code?
- How Apex works on the Salesforce Platform?
- Apex variables, constants, data types, collections, and enums.
- Apex classes, methods, constructors, interfaces, and access modifiers.
- Conditional statements, looping statements, and exception handling.
- SOQL and SOSL query basics.
- DML operations and transaction control.
- Apex triggers and trigger handler patterns.
- Apex governor limits and bulkification.
- Apex test classes, assertions, and code coverage.
- Batch Apex, Queueable Apex, Scheduled Apex, and Future methods.
- Apex debugging with logs and checkpoints.
- Deployment of Apex from sandbox to production.
For interview preparation, refer to SFDC Apex Interview Questions and Visualforce Interview Questions.
Official Salesforce Apex references for further learning
Use official Salesforce material when checking syntax, limits, deployment rules, and current platform behavior. Good references include the Salesforce Developer Guide page on what Apex is, the Apex Developer Guide, and the Trailhead Apex Basics and Database module.
Common mistakes while learning Salesforce Apex
- Writing Apex for every requirement: Start with declarative Salesforce tools when they can solve the problem cleanly.
- Ignoring bulk data: Code that works for one record can fail when a trigger receives many records.
- Using SOQL or DML inside loops: This is one of the fastest ways to hit governor limits.
- Chasing only 75% coverage: Passing tests with meaningful assertions are more valuable than coverage-only tests.
- Putting all logic in triggers: Keep triggers small and move business logic into classes.
- Not considering security: Understand sharing, field-level security, object permissions, and user context when writing Apex.
FAQ on Salesforce Apex tutorials and Apex programming language
Is Salesforce Apex a programming language?
Yes. Salesforce Apex is a programming language used to write custom server-side logic on the Salesforce Platform. It is strongly typed, object-oriented, and closely integrated with Salesforce data and metadata.
Is Apex programming easy to learn for beginners?
Apex is easier to learn if you already understand Java, C#, or another object-oriented language. Beginners can still learn Apex, but they should study Salesforce objects, SOQL, DML, governor limits, and test classes along with syntax.
What coding skills are needed for Salesforce Apex?
You need basic object-oriented programming, variables, loops, conditions, collections, exception handling, database thinking, and testing skills. Salesforce-specific skills include SOQL, DML, triggers, sharing, governor limits, and deployment practices.
What programming language should I learn for Salesforce development?
For backend Salesforce development, learn Apex. For frontend Lightning development, learn JavaScript and Lightning Web Components. For configuration-heavy work, also learn Flow, validation rules, formulas, and the Salesforce data model.
Can Apex code be written directly in a production Salesforce org?
In normal development practice, Apex is written and tested in a developer org or sandbox, then deployed to production with passing tests and required coverage. This protects production data and helps catch errors before release.
Editorial QA checklist for this Salesforce Apex tutorial
- Does the tutorial define Salesforce Apex as a server-side Salesforce programming language without overstating what it can do?
- Does every Apex example use safe beginner patterns, especially no SOQL or DML inside a loop?
- Are Developer Console, VS Code, Salesforce CLI, sandbox, and production deployment described with current Salesforce terminology?
- Does the testing section explain that 75% coverage is a minimum deployment requirement, not a complete testing strategy?
- Do the FAQ answers directly address beginner questions from the Salesforce Apex search intent?
TutorialKart.com