Apex is Salesforce’s proprietary, strongly typed programming language used to execute server-side logic. Apex classes allow developers to extend Salesforce functionality by implementing business logic, integrations, triggers, controllers, and batch processing.
In this article, we will learn how to create an Apex class in Salesforce using two common methods:
- Developer Console
- Visual Studio Code with Salesforce Extensions
Both approaches are widely used depending on your development workflow.
What Is an Apex Class?
An Apex class is a blueprint that contains:
- Variables
- Methods
- Business logic
Apex classes are used in:
- Triggers
- Lightning Web Components (LWC)
- Visualforce pages
- Batch jobs
- Integration logic
Creating Apex classes correctly is essential for building scalable Salesforce applications.
Method 1: Create an Apex Class Using Developer Console
This method is simple and best suited for beginners or quick development tasks.
Step 1: Log in to Salesforce
- Open your web browser
- Log in to your Salesforce org (Developer, Sandbox, or Production)
Step 2: Access Developer Console
- Click on the App Launcher (grid icon)
- Search for Developer Console
- Open the Developer Console in a new window
Step 3: Create a New Apex Class
- In the Developer Console menu, go to:
- File → New → Apex Class
- File → New → Apex Class
- Enter a class name (for example: MyApexClass)
- Click OK
Step 4: Write Apex Code
Once the editor opens, write your Apex logic.
Below is a simple example:
public class MyApexClass {
public String greeting {
get {
return ‘Hello, Salesforce!’;
}
}
}
This example demonstrates:
- A public Apex class
- A property with a getter method
- Returning a string value
Step 5: Save the Apex Class
- Click File → Save
- Or use shortcut keys:
- Ctrl + S (Windows)
- Cmd + S (Mac)
- Ctrl + S (Windows)
Step 6: Compile and Check for Errors
- Go to Run → Compile All Classes
- Check the Problems tab for compilation errors
- Fix any issues if errors appear
Step 7: Use the Apex Class
Once compiled successfully, the Apex class can be used in:
- Triggers
- Controllers
- Batch Apex
- Schedulers
- REST services
Method 2: Create an Apex Class Using Visual Studio Code (VS Code)
This is the recommended method for professional Salesforce development, especially when working with source control and multiple developers.
Step 1: Install Visual Studio Code
- Download and install Visual Studio Code
- Ensure it is properly installed on your system
Step 2: Install Salesforce Extensions
- Open VS Code
- Go to the Extensions Marketplace
- Install Salesforce Extension Pack
This extension provides:
- Apex development support
- CLI integration
- Code completion and validation
Step 3: Open Your Salesforce Project
- Open your SFDX Salesforce project in VS Code
- Make sure your org is authenticated
Step 4: Create a New Apex Class
- Click on the Explorer icon
- Right-click on the project folder
- Select SFDX: Create Apex Class
- Enter the class name when prompted
- Choose the default directory
Step 5: Write Apex Code
VS Code will automatically generate a basic class structure.
You can now add your logic inside the class.
Step 6: Save the Apex Class
- Save the file using:
- File → Save
- Or Ctrl + S / Cmd + S
- File → Save
Step 7: Deploy Apex Class to Salesforce
You can deploy the Apex class using:
- Salesforce CLI commands
- VS Code Salesforce extension options
Once deployed, the class becomes available in your Salesforce org.
Best Practices for Creating Apex Classes
- Use meaningful and descriptive class names
- Always follow bulk-safe design
- Avoid hardcoding values
- Write test classes for every Apex class
- Keep logic modular and reusable
- Follow Salesforce governor limits
Conclusion
Creating an Apex class is a foundational skill for Salesforce developers. Whether you use the Developer Console for quick tasks or Visual Studio Code for enterprise development, understanding both methods gives you flexibility and control.
By mastering Apex classes, you unlock the ability to build powerful business logic, integrations, and custom automation within Salesforce.


Nice post
Pingback: How to use loop in apex Salesforce O - 9to9clouds