Understanding Apex in Salesforce

Black BUCBlack BUC
5 min read

👨‍💻 What is Apex?

Apex is a strongly typed, object-oriented programming language developed by Salesforce.com. It is designed specifically for developers to write custom code that executes directly on the Force.com platform.

🧩 Key points:

  • Apex is proprietary to Salesforce and is used primarily for building SaaS applications.

  • The code is saved, compiled, and executed on Salesforce servers.

  • It allows developers to write custom logic that executes during system events like button clicks, record updates, and triggers.


⚙️ Features of Apex

Apex offers several features that make it efficient and integrated for enterprise-grade applications:

🔄 Automatic Upgrades
➡️ Apex gets automatically updated with Salesforce releases—no manual intervention needed.

🛢️ Built-in Database Integration
➡️ You can query and manipulate Salesforce records directly without manually setting up DB connections.

📝 Java-like Syntax
➡️ Developers familiar with Java will find Apex intuitive and easy to learn.

🧪 Built-in Testing Support
➡️ Apex includes native support for writing and executing unit tests, which is also mandatory for deployment.

🏢 Multi-Tenant Architecture
➡️ Supports a shared environment while ensuring data separation between tenants.

📌 API Version Control
➡️ Apex code can be locked to specific versions of the Salesforce API.

🔡 Case-Insensitive
➡️ Apex treats keywords and identifiers in a case-insensitive manner.


🔍 When to Use Apex?

Use Apex when business requirements go beyond what declarative tools (like Flow or Process Builder) can handle:

✔️ To implement complex business logic
✔️ To perform cross-object validations
✔️ To create custom web services or email handlers
✔️ To manage database transactions and rollbacks

Apex gives precise control over the logic and behavior of the application, especially in complex or large-scale environments.


📦 What is a Class in Apex?

In Apex, a class is a fundamental building block used to define custom logic and structure:

🔹 A class is a template from which objects are created.
🔹 It can contain variables (fields) and methods (functions).

🧱 Example:

If Animal is a class, then:

  • Dog and Cat can be objects (instances) of this class.

  • The class may include properties like species and methods like speak().


5. How to Create a Class in Apex?

In Apex, a class is like a blueprint that contains methods and variables. It defines the structure and behavior of an object.

📌 Syntax of an Apex Class:

Access_Modifier class Class_Name {
    // Your code here
}

🔍 Example:

public class StudentManager {
    // Code inside the class
}

🧠 Note:

  • public is the access modifier.

  • class is a keyword.

  • StudentManager is the name of the class.


🧩 6. How to Write a Method/Function Inside a Class?

A method is a block of code designed to perform a particular task. It resides inside a class.

📌 Syntax of a Method:

Access_Modifier Return_Type Method_Name() {
    // Method body
}

🔍 Example:

public class StudentManager {
    // This is a method
    public void calculateGrades() {
        System.debug('I am in line 4');
    }
}

💡 Tips:

  • void means the method doesn't return anything.

  • System.debug() helps display a message in the logs.


🗨️ 7. What Are Comments in Apex?

Comments are non-executable lines in code used for description or for temporarily removing parts of code during testing.

📝 Types of Comments:

1️⃣ Single-Line Comment:

// This is a single-line comment.

2️⃣ Multi-Line Comment:

/*
This is a multi-line comment.
Used for longer descriptions.
*/

👍 Comments help improve readability and collaboration in code.


🧪 8. Example Code Discussed in the Session

Here’s a practical example discussed:

public class FirstExample {
    // This method is just debugging some statements.
    public void printInfo() {
        /*
        System.debug('This is my first debug statement');
        System.debug('This is my first debug statement');
        */
    }
}
System.debug('This is my second debug statement');

▶️ How to Call the Above Class Method using the Anonymous Window:

FirstExample exampleObj = new FirstExample();
exampleObj.printInfo();

🎭 9. What is an Object of a Class in Apex?

An object is an instance of a class. When you create an object, you’re telling Salesforce to reserve memory and make all the class features available through that object.

📌 Syntax for Creating an Object:

ClassName objectName = new ClassName();

🔍 Example:

public class ReportGenerator {
    // class body
}
ReportGenerator reportObj = new ReportGenerator();

📍 Here:

  • ReportGenerator is the class.

  • reportObj is the object.

  • new allocates memory.


🐞 10. What is System.debug()?

System.debug() is a powerful tool used to print values or track code execution. It helps developers identify issues and verify logic.

🖥️ Example:

System.debug('This is a debug message');

🧰 Use Case:
When testing your code in the Developer Console, check the logs to see debug statements and understand the code flow.


🎓 Summary

ConceptDescription
ClassBlueprint for objects
MethodFunction inside a class
CommentsNon-executed code used for description
ObjectInstance of a class
System.debug()Used for printing messages for debugging

📚 Keep practicing, and you'll soon be building powerful applications in Apex!
💬 Have questions? Try writing a small class and method today and debug your output using System.debug()!


0
Subscribe to my newsletter

Read articles from Black BUC directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Black BUC
Black BUC