Grab Month-end Scholarship + Register with best Offer
00D 00H 00M 00S
×

Grab Month-end Scholarship +
Best Offer!

00D 00H 00M 00S

Salesforce Apex Class Tutorial: From Basics to Advanced

Sanjeeta
By Sanjeeta
Salesforce 01 Apr 2026 | Last Updated: 01 Apr 2026

This blog covers Salesforce Apex classes from basic concepts to advanced techniques in a simple, step-by-step way. It explains class structure, methods, OOP concepts, and real-world examples for practical learning. Perfect for beginners and developers looking to improve their Salesforce coding skills.

Salesforce Apex Class Tutorial: From Basics to Advanced
Salesforce Apex Class Tutorial Explained
Table of Contents +

    Salesforce is the world’s leading cloud platform that helps organizations manage customers, sales and productivity. It has a lot of built-in functionality but most organizations will want some custom functionality specific to their workflows and that’s where Apex comes into play. Apex is a Salesforce-language that enables you to add your own business logic and automation beyond the out of the box capabilities. And at the center of all this are Apex classes, the fundamental building blocks for writing clean, useful, modular code. If you want to be a salesforce developer then how Apex class works is one of the essential things which you need to know, and this is often covered in detail in Salesforce courses online. Apex also enables you to develop high performance, scalable applications.


    What Is an Apex Class in Salesforce? 


    The class in apex is a class which is used to write the business logic and you can create objects of this class in triggers, other classes or pages. If you have experience in Java or C++, Apex will be familiar as it includes many of the same object-oriented constructs such as encapsulation, inheritance, and code reuse. An Apex class is like a blueprint and the objects are instances of that blueprint however, there are several layers of code organisation that will help you keep your code clean, readable and maintainable. You can query, modify records, and even run business processes. Apex classes allow you to manage tasks behind the curtain including executing them asynchronously or validating data prior to saving it or connecting Salesforce with outside applications.If standard features are not sufficient for your needs, you can also use Apex to build custom solutions for complex business requirements.   

    Key Features of Apex Classes 


    • Strongly typed language: In Apex, you are always required to declare the type of a variable. It’s a little bit rigid at the beginning, but it really allows you to find errors earlier and makes your code more resilient.   
    • Object oriented: Apex is an object oriented environment and we get the usual OOP features such as encapsulation, inheritance and polymorphism which help in making your code cleaner, more organized and easy to reuse.
    • Tailored for Salesforce environment: As it is designed to run on Salesforce’s multi-tenant architecture, Apex is designed for high efficiency and to be resource friendly while sharing resources securely among different users and organizations.
    • Basic data manipulation: Native support for SOQL and DML allows you to elegantly fetch and update Salesforce records whether you are processing them, or persisting them. 
    • Works within limits: Apex has resource limits known as governor limits, and developers are taught to write optimized and efficient code from day one.  

    Structure of an Apex Class


    An Apex class is well defined and structured so it allows to better customize the behavior of Salesforce. In its simplest form a class can be declared as public, private or global depending on the accessibility and usage of the class. In a class, you normally have data variables and methods that operate on that data. There are also constructors, a special type of method which enables us to set the initial state of the object when we create an instance of the class. 

    Access modifiers are crucial here as they define the visibility of the classes, variables, and methods and can be employed to protect sensitive logic and to a certain extent enforce a neater code. And then as one of the other core concepts is static methods versus instance methods. Static methods are called on the class, not on an object, and instance methods are called on an object of the class. Knowing this about the architecture will also help you when you are refactoring and tuning your Apex. 

    Types of Apex Classes in Salesforce

    There are various categories of Apex classes in Salesforce related to different use cases. That simple statement on when to use what, should make development so much easier. 
    • Standard classes are the classes which are provided by salesforce.They come with built-in functionality (such as handling collections or dates) so that you don’t have to reinvent the wheel. 
    • Customize classes allow developers to perform any type of business or application logic. These are the most popular, but may be adjusted to your needs.
    • Controller classes are used with Visualforce pages, Lightning Web Components (LWC), or Aura components to process user actions and to bridge the UI and backend.
    • Test classes are necessary for deployment. Sales force mandates a minimum of 75% code coverage, so test classes are created to test that your code executes properly.
    • To process big data, Batch Apex Classes processes records as batches. While Queueable and Future classes have been employed to perform asynchronous processing to execute tasks behind scenes and not blocking the system. 

    How Apex Classes Work in Salesforce?


    Salesforce has different execution contexts for Apex code which depend on how the code is run. Sometimes they are synchronous, the code runs now and the user waits for the result, other times they are asynchronous, processing occurs in the background and overall performance is better. These classes work very closely with the salesforce database. Developers use SOQL to query for data and DML operations to insert, update, delete records as business logic, skills that are typically reinforced through Salesforce courses online with hands-on exercises.

    Apex classes can be invoked in a number of ways including from the user interface, through the API (including web services); and from triggers (automatically executed when data is modified). They also work natively with Lightning components, as well as with external APIs, allowing developers to build dynamic user experiences connect Salesforce to other systems.  

    Difference Between Apex Class and Apex Trigger 

    An Apex trigger is a script that is executed before or after a DML event occurs, such as insert, update, or delete on an object in Salesforce. 

    Here’s a simple comparison:

    Feature

    Apex Class

    Apex Trigger

    Execution

    Called manually or programmatically

    Runs automatically on data events

    Purpose

    Reusable business logic

    Event-driven actions on records

    Use Cases

    Integrations, automation, logic

    Data validation, automation on save

    Flexibility

    More flexible and reusable

    Limited to specific object events

    Scalability

    Easier to scale and maintain

    Can become complex if not managed well


    In summary, Apex classes can contain reusable logic whereas triggers execute immediately in response to data changes. 

    Basic Example of an Apex Class 


    Here's a simple and clear example of Apex class for Account management in salesforce: 
    public class AccountManager {
        
        public static void updateAccountPhone() {
            
            // SOQL Query to get an Account
            Account acc = [SELECT Id, Name, Phone FROM Account LIMIT 1];
            
            // Modify the phone number
            acc.Phone = '123-456-7890';
            
            // DML operation to update the record
            update acc;
        }
    }

    Step-by-step explanation:


    1. Class Declaration

    public class AccountManager defines a class named AccountManager.
    “public” means it can be accessed from anywhere in Salesforce.

    2. Method

    public static void updateAccountPhone() is a method.
    • static means it can run without creating an object.
    • void means it doesn’t return anything.

    3. SOQL Query

    [SELECT Id, Name, Phone FROM Account LIMIT 1] fetches one Account record from the database.

    4. DML Operation

    update acc; saves the changes back to Salesforce.

    What the code does:

    This class fetches an Account, modifies its phone number and updates it. It's a simple demonstration of reading and updating data in Salesforce with Apex. 

    Best Practices for Writing Apex Classes 

    Following are best practices to writing Apex classes in Salesforce: 
    • Apply Standard Naming conventions for Apex class in salesforce to get clear and consistent code.
    • Make the code modular and reusable so the same logic can be applied across different pages / components (visualforce), something you’ll often practice in Salesforce courses online.
    • Make your code bulkable, to work on multiple records at a time rather than one at a time.
    • Use exception handler (try-catch) for preventing runtime error and improving stability.
    • Your test classes should cover at least 75% of your code as a requirement from Salesforce.
    • Please consider the governor limits and try to optimize your code by minimizing the number of SOQL queries and DML statements inside the for loops. 

    Common Mistakes to Avoid

    Here are some commonly made mistakes while working with Apex class in salesforce: 

    • Ignoring governor limits which may lead to failure of the Apex class in Salesforce while run.
    • Writing non-bulkified code that processes only one record at a time instead of handling lists.
    • Lack of test coverage, making it impossible to deploy Apex class in Salesforce to production.
    • Hardcoding values instead of using custom settings or metadata, reducing flexibility.
    • Not handling exceptions properly, which can lead to unexpected errors and poor user experience.

    Conclusion 


    Salesforce Apex Class is significant in the business process to modify and automate the business logic of the salesforce platform. By using Apex class execution, developers gets an option to write the code and run it on the server side. From complex data manipulation to executing complex business logic, Apex lets you extend Salesforce well out beyond the standard UI. Knowing what not to do, and the best practices, you can build solutions that are bulletproof. Practice, practice, practice consistently, and when you get a chance go into further advanced thoughts like triggers and integrations. Start your journey with structured learning through Salesforce courses online at Srijan Institute.

    FAQs Related to Salesforce Apex Class


    Q1. What is an Apex class in Salesforce used for?

    A. A Salesforce Apex class is quite similar to java class which contains variables and methods that can be used for executing some operations including writing some business logic. Instead of taking Salesforce as is, developers could now customize it for their needs.  

    Q2. What differentiates an Apex class from a trigger? 

    A. The Salesforce Apex Class is concrete and can be reused.  Apex triggers operate on events in the database like insert, update, delete, and they too are implicitly executed. 

    Q3. Can beginners learn Apex classes easily?

    A. Yes, beginners can learn Apex class in salesforce easily, more so if they have some programming knowledge.With proper guidance, they can grasp fundamental concepts very quickly and immediately write simple programs. .

    Q4. What are the different types of Apex classes?

    A. Salesforce apex classes can be standard, custom, controllers, test controllers, batch, asynchronous etc. All of these are utilized for aspects of process automation or application building and each type serves a different purpose. 

    Q5. How do you deploy the apex class in salesforce?

    Salesforce developers deploy an apex class in salesforce through change sets, salesforce CLI or devops tools. Rust deployment needs 75% test coverage for the code to be able to be trusted. 

    WhatsApp
    WhatsApp