Salesforce is a robust Customer Relationship Management (CRM) platform allowing businesses to manage their customer relationships efficiently. One key element that makes Salesforce so powerful is its ability to be customized and extended through Apex. If you are new to Salesforce or looking to deepen your understanding of Apex, this tutorial will provide a comprehensive guide to get you started. This page contains all the Apex tutorials in Salesforce.
What is Apex in Salesforce?
Apex is a strongly typed, object-oriented programming language that Salesforce developers use to execute flow and transaction control statements on the Salesforce platform. It allows developers to add custom business logic to system events such as button clicks, related record updates, and Visualforce pages.
Key Features of Apex
- Integrated with Salesforce: Apex is deeply integrated with Salesforce, allowing developers to access Salesforce data and execute complex business processes directly within the Salesforce environment.
- Strongly Typed: Apex is a strongly typed language, meaning variables must be declared with a specific type, reducing errors and increasing code reliability.
- Object-Oriented: Apex supports classes, inheritance, polymorphism, and other object-oriented concepts, making it a powerful tool for developers familiar with these paradigms.
- Built-in Support for DML Operations: Apex has built-in support for Data Manipulation Language (DML) operations, making it easy to insert, update, delete, and query Salesforce records.
- Governor Limits: Salesforce enforces governor limits to ensure that no single Apex execution monopolizes shared resources. This encourages efficient and scalable code.
Get Started with Apex in Salesforce
Now, let me show you how to get started with Apex in Salesforce.

Setting Up Your Development Environment
Before you start writing Apex code, you need to set up your Salesforce development environment. Here’s a quick guide to get you started:
- Sign Up for a Salesforce Developer Account: Go to the Salesforce Developer website and sign up for a free developer account. This account provides a fully functional Salesforce environment to write and test your Apex code.
- Install Salesforce Extensions for Visual Studio Code: Visual Studio Code (VS Code) is a popular code editor that supports Salesforce development. Install the Salesforce extensions for VS Code to enable syntax highlighting, code completion, and other features for Apex development.
- Connect VS Code to Your Salesforce Org: Use the Salesforce CLI to authenticate and connect your VS Code environment to your Salesforce org. This allows you to deploy and retrieve Apex code directly from your editor.
Write Your First Apex Class
Now that your development environment is set up, let’s write your first Apex class. Open VS Code and create a new Apex class file with the following code:
public class HelloWorld {
public static void sayHello() {
System.debug('Hello, Salesforce!');
}
}This simple class defines a method sayHello that outputs a debug message to the Salesforce debug log. To execute this method, you can use the Salesforce Developer Console:
- Open the Salesforce Developer Console from your Salesforce org.
- Click on Debug > Open Execute Anonymous Window.
- Enter the following code and click Execute:
HelloWorld.sayHello();Check the Logs tab in the Developer Console to see the output message.
Apex Syntax and Structure
Apex syntax is similar to Java and C#, making it familiar to developers with experience in these languages. Here are some key elements of Apex syntax and structure:
- Classes and Methods: Apex code is organized into classes and methods. Classes are blueprints for objects, and methods define the behavior of those objects.
- Variables and Data Types: Apex supports various data types, including primitives (e.g.,
Integer,String), collections (e.g.,List,Set), and complex types (e.g.,sObject). - Control Statements: Apex includes standard control statements such as
if-else,for,while, andswitchfor flow control. - Exception Handling: Apex provides try-catch blocks for exceptions, allowing developers to manage errors gracefully.
Check out What Language Does Salesforce Use
Working with Salesforce Data with Apex
One of the most powerful features of Apex is its ability to interact with Salesforce data. Here are some common operations you can perform:
Querying Data with SOQL
SOQL (Salesforce Object Query Language) is used to query Salesforce data. Here’s an example of a simple SOQL query:
List<Account> accounts = [SELECT Id, Name FROM Account WHERE Industry = 'Technology'];
for (Account acc : accounts) {
System.debug('Account Name: ' + acc.Name);
}This query retrieves all accounts in the technology industry and outputs their names to the debug log.
Manipulating Data with DML
Apex provides DML operations to manipulate Salesforce data. Here are some examples:
- Insert: Create a new record.
Account newAccount = new Account(Name = 'New Account');
insert newAccount;- Update: Modify an existing record.
Account existingAccount = [SELECT Id, Name FROM Account WHERE Name = 'Existing Account' LIMIT 1];
existingAccount.Name = 'Updated Account';
update existingAccount;- Delete: Remove a record.
Account accountToDelete = [SELECT Id FROM Account WHERE Name = 'Account to Delete' LIMIT 1];
delete accountToDelete;Triggers in Apex
Triggers are a powerful feature in Apex that allows you to execute code in response to specific events on Salesforce records, such as insertions, updates, or deletions. Here’s an example of a simple trigger:
trigger AccountTrigger on Account (before insert, before update) {
for (Account acc : Trigger.new) {
if (acc.Industry == 'Technology') {
acc.Description = 'This is a technology account.';
}
}
}This trigger updates the description of technology accounts before they are inserted or updated.
Best Practices for Apex Development
To ensure your Apex code is efficient, maintainable, and scalable, follow these best practices:
- Bulkify Your Code: Use collections and loops to ensure your code can handle multiple records at once. This helps you stay within Salesforce governor limits.
- Use Efficient SOQL Queries: Avoid querying more data than necessary and use filters to minimize the number of records returned.
- Handle Exceptions Gracefully: Use try-catch blocks to handle exceptions and provide meaningful error messages.
- Write Unit Tests: Salesforce requires at least 75% code coverage for deployment. Write comprehensive unit tests to ensure your code works as expected.
- Follow Naming Conventions: Use meaningful names for classes, methods, and variables to make your code more readable.
Salesforce Apex Tutorials
Here is the list of Salesforce apex tutorials.
- How to Connect VS Code to Salesforce Org
- Unable to Activate the Apex Language Server
- System.LimitException: Too Many SOQL Queries
- Variables and Data Types in Salesforce Apex
- How to Get Current User Info Using Apex in Salesforce
- How to Create a New User Using Salesforce Apex
- How to Activate or Deactivate User From Salesforce Apex
- How to Reset and Change User Password Using Salesforce Apex
- How to Convert a String to an Integer using Apex in Salesforce
- How to Convert String to Date in Salesforce Apex
- How to Convert String to ID in Salesforce Apex
- How to Convert String to Boolean in Salesforce Apex
- How to Convert a String to Decimal in Salesforce Apex
- How to Convert String To Lowercase And Uppercase In Salesforce Apex
- How to Convert String to Blob in Salesforce Apex
- How to Split String Method in Salesforce Apex
- How to Get Current Datetime in Apex Salesforce
- How To Get The Picklist Value In Salesforce Apex Class
- How to Use Switch Statement in Salesforce Apex
- How to Check if a String is Null, Empty or Blank in Salesforce Apex
- How to Add User to Group or Permission Set Using Salesforce Apex
- How to Convert ID to String in Salesforce Apex
- How to Convert String to Datetime in Salesforce Apex
- How to Convert Integer Data Type to Decimal in Salesforce Apex
- How to Convert Integer Data Type to String in Salesforce Apex
- How to Replace String in Salesforce Apex
- How to Remove First and Last Character from a String in Salesforce Apex
- How to Create List Collection in Salesforce Apex?
- How to Create Set Collection in Salesforce Apex?
- How to Create Map Collection in Salesforce Apex?
- How to Reverse a String in Salesforce Apex
- How to Convert List to String in Salesforce Apex
- How to Convert Date to String in Salesforce Apex
- How to Convert JSON to String in Salesforce Apex
- How to Convert String to Double in Salesforce Apex
- How to Extract Year, Month, and Day from a Date Field in Salesforce Apex
- How To Schedule Jobs Using the Apex Scheduler in Salesforce
- How to Convert DateTime to Date Format in Salesforce Apex
- How to Create SObject in Salesforce Apex
- Logical and Looping Statements in Salesforce Apex Programming
- Apex Triggers in Salesforce
- How to Compare Two DateTime Values in Salesforce Apex
- Apex Trigger Handler and Helper Class in Salesforce
- Trigger.New in Salesforce Apex
- Trigger.Old in Salesforce Apex
- How to Create and Use Constructors in Salesforce Apex Classes
- Object Oriented Programming in Salesforce Apex
- Inheritance in Salesforce Apex
- Interfaces in Salesforce Apex and How to Use it
- Polymorphism in Salesforce Apex
- Abstraction in Salesforce Apex
- Ternary Operator in Salesforce Apex
- Safe Navigation Operator in Salesforce Apex
- How to Merge Records in Salesforce with Apex
- Access Modifiers in Salesforce Apex Classes
- How to Create Wrapper Class in Salesforce Apex
- Dependency Injection in Salesforce Apex Class
- Apex String Methods to Determine Character Types in Salesforce
- Exception Handling in Salesforce Apex
- Different Types of Exceptions in Salesforce Apex
- Salesforce Object Query Language (SOQL) in Apex
- ORDER BY Clause in SOQL
- GROUP BY Clause in SOQL
- GROUP BY Date Clause in SOQL
- DATE Clause in SOQL
- Salesforce Relationship Queries in SOQL
- LIKE Clause in SOQL
- OFFSET Clause in SOQL
- Dynamic Queries in SOQL (Salesforce Object Query Language)
- What is Schema Class in Salesforce Apex and How to Use it
- Asynchronous Apex in Salesforce
- How to Avoid Recursion in Salesforce Apex Triggers
- Aggregate Query in Salesforce SOQL
- HAVING Clause in SOQL
- For Clause in SOQL
- What is the Difference between SOQL and SOSL in Salesforce
- How to Avoid SOQL Injection in Salesforce
- How to Query Salesforce Picklist Field values using SOQL
- Salesforce Field Security in SOQL WITH Security Enforced
- How to Access Custom Label in Salesforce Apex?
- SOQL Governor Limits in Salesforce
- Salesforce SOQL Distinct Queries
- Selective SOQL Queries in Salesforce
- Salesforce SOQL Inner Join and Outer Join Relationships
- SOQL vs SQL Difference in Salesforce
- Salesforce SOQL Logical Operators
- How to Query SOQL NOT LIKE Operator in Salesforce
- Salesforce SOQL Query RecordType
- Query Tasks Notes Attachments by Object Type using SOQL in Salesforce
- Create a For Loop to Iterate Through a SOQL Query
- SOQL Query on Group and GroupMember in Salesforce
- Validate CRUD permission before SOQL/DML operation in Salesforce
- How to Create a Mock Callout to Test the Apex Rest Callout in Salesforce
- Track Object Field History in Salesforce using SOQL
- Custom Iterator Interface in Salesforce Apex
- Get User Or Logged-In User Details Using SOQL
- How to Convert a JSON String to a Map in Salesforce Apex
- SOQL Query in Apex Programming
- How to Convert String to sObject in Salesforce Apex
- Query to Count Child Records in Salesforce SOQL
- Alias Notations in Salesforce SOQL
- Salesforce Big Object and Async Query
- How to Use Apex Controllers with Visualforce Pages?
- Create Forms using Visualforce Page in Salesforce
- Generate PDFs in Salesforce with Visualforce Pages
- How to Implement Pagination in Salesforce Visualforce Pages
- Visualforce Page Tags in Salesforce
- How to Add a Visualforce page in Salesforce Lightning Pages?
- What are Visualforce Components in Salesforce?
- How to Embed an Image in Salesforce Visualforce Pages?
- Embed Reports Charts and Dashboards to Visualforce Pages in Salesforce
- How to Find Deleted Records in Salesforce using SOQL
- Salesforce SOQL toLabel() Function
- How to Return Map Result from SOQL Query in Salesforce Apex
- Salesforce SOQL and SOSL Limits for Search Queries
- How do you query activity related to fields with SOQL in Salesforce?
- How to Query Validation Rule in Salesforce?
- Query Records Created Today or Yesterday with SOQL in Salesforce
- Null Coalescing Operator in Salesforce
- How do you query multi-currency fields in Salesforce SOQL?
- How to Convert TimeZone Values in Salesforce?
- IN Operator/Clause in SOQL(Salesforce Object Query Language)
- How to Query the ListView of an Object in Salesforce SOQL?
- How to Use Bind Variables in Salesforce SOQL?
- Batch Apex in Salesforce With Examples
- Scheduler Apex in Salesforce With Examples
- Queueable Apex Chaining in Salesforce With Examples
- How to Call Apex Class From Salesforce Flow
- Salesforce Object Search Language (SOSL) in Apex
- How to Create Dynamic SOSL Query in Salesforce Apex
- Data Manipulation Language(DML) in Salesforce Apex
- CANNOT EXECUTE FLOW TRIGGER Error With DML Operation in Salesforce
- Salesforce DML Before and After Callout
- Send An Email to User and Public Group Using Apex in Salesforce
- Salesforce Apex With, Without, Inherited and Omitted Sharing
- Virtual Class in Salesforce Apex
- How to Clone the Record With Related List in Salesforce
- Trigger Handler Pattern in Salesforce Apex
- Assign Tasks in Salesforce Using Apex
- Trigger Context Variable in Salesforce Apex
- String Class Methods in Salesforce Apex
- Convert Apex List to Set and Map Collections in Salesforce
- addError() Method in Salesforce Apex
- Write a Test Class For an Apex Trigger in Salesforce
- Bypass Duplicate Rules Using Salesforce Apex
- Create a Visualforce Page with Apex Controller in Salesforce
- Add Visualforce Page Tab in Salesforce
- Create User Registration Form Using Salesforce Visualforce
- File Upload Functionality in Salesforce: Visualforce & Apex
- Display Custom Error Messages in Salesforce Apex
- Use Visualforce with Lightning Web Components in Salesforce
- Before vs After Triggers in Salesforce: When to Use & Why
- Restrict Record Deletion Using Salesforce Trigger
- Before Insert Validation in Salesforce Trigger [With Examples]
- Create Rollup Summary on Lookup Relationship Using Salesforce Apex Trigger
- Auto-Populate Fields Using Apex Trigger in Salesforce
- Trigger Helper Class in Salesforce
- Loops in Salesforce Apex: For, While, Do-While Examples
- Salesforce Apex If Else, Nested If, and Switch Case Examples
- Collections in Salesforce Apex [List, Set, & Map Explained]
- String Class in Apex Salesforce: Complete Guide with Methods & Examples
- String SOQL Queries in Salesforce Apex [Complete Guide with Examples]
- Automate Processes Using Apex Triggers in Salesforce [Detailed Guide]
- Trigger.newMap & Trigger.oldMap Context Variable in Apex Trigger
- Get Record Type ID in Salesforce Apex
- Salesforce: Call Apex From Lightning Web Components (LWC)
- Contains() Method in Salesforce Apex
Conclusion
Apex is a powerful tool that allows Salesforce developers to add custom business logic to their applications. You can start building custom solutions that enhance your Salesforce org by understanding the basics of Apex syntax, data manipulation, and triggers. Remember to follow best practices to ensure your code is efficient and maintainable. With practice and experience, you’ll become proficient in Apex and unlock the full potential of Salesforce for your organization.