Contents
- Introduction
- Triggers
- Bulk Triggers
- Trigger Context Variables
- Order of Execution
- Test Classes
- Conditional Statements
- Asynchronous Apex
- Queueable Apex
- Apex Scheduler
- Batch Apex
- Future Methods
- SOAP Web Services
- REST Web Services
- Integration and Apex Utilities
- Debugging
- Testing Apex
- Deploying Apex
Introduction
Apex is an Object Oriented Programming language, which is not a case sensitive. It allows developers to execute the flow and transaction control statements on the Force.com platform server in conjunction with calls to the Force.com API.
| Available in: Salesforce Classic (not available in all orgs) and Lightning Experience |
| Available in: Enterprise, Performance, Unlimited, Developer, and Database.com Editions |
Apex Code Development Tools
In all the editions, we can use any of the following three tools to develop the code −
- Force.com Developer Console
- Force.com IDE
- Code Editor in the Salesforce User Interface
Why we use Apex?
Apex should be used when we are not able to implement the complex business functionality using the pre-built and existing out of the box functionalities. Below are the cases where we need to use apex over Salesforce configuration.
We can use Apex when we want to −
- Create Web services with integrating other systems.
- Create email services for email blast or email setup.
- Perform complex validation over multiple objects at the same time and also custom validation implementation.
- Create complex business processes that are not supported by existing workflow functionality or flows.
- Create custom transactional logic (logic that occurs over the entire transaction, not just with a single record or object) like using the Database methods for updating the records.
- Perform some logic when a record is modified or modify the related object’s record when there is some event which has caused the trigger to fire.
How Apex Works?
All Apex runs entirely on-demand on the Lightning Platform. Developers write and save Apex code to the platform, and end users trigger the execution of the Apex code via the user interface.
Programmatic Elements in Apex


Let’s write a simple class (Hello World) in Apex
public class MyHelloWorld {
public static void applyDiscount(Fee__c[] fees) {
for (Fee__c f :fees){
f.totalAmount__c*= 0.9;
}
}
}
Now, we will look into each line of the code!
public class MyHelloWorld {}
Here MyHelloWorld is the class name.
public static void applyDiscount(Fee__c[] fees) {}
applyDiscount() is a public and Static class method, which means the class is available to other Apex classes and triggers. Because it is a static method, you don’t need to create an instance of the class to access the method—you can just use the name of the class followed by a dot (.) and the name of the method.
This method takes one parameter, a list of Fee records, which is assigned to the variable fees. Notice the __c in the object name Fee__c. This indicates that it is a custom object that you created. Standard objects that are provided in the Salesforce application, such as Account, don’t end with this postfix.
Next line of code contains rest of the method:
for (Fee__c f :fees){
f.totalAmount__c*= 0.9;
}
- Notice the __c after the field name TotalAmount__c. This indicates it is a custom field that you created.
- Standard fields that are provided by default in Salesforce are accessed using the same type of dot notation but without the __c, for example, Name doesn’t end with __c in Fee__c.Name.
- The statement f.totalAmount__c *= 0.9; takes the old value of f.totalAmount__c, multiplies it by 0.9, which means its value will be discounted by 10%, and then stores the new value into the f.totalAmount__c field. The *= operator is a shortcut.
- Another way to write this statement is f.totalAmount__c = f.totalAmount__c * 0.9;.
Now, save this class and we will call applyDiscount() class method in to trigger.
Apex Design Best Practices
Apex Code is the Force.com programming language used to write custom, robust business logic. As with any programming language, there are key coding principles and best practices that will help you write efficient, scalable code. This article illustrates many of the key best practices for writing and designing Apex Code solutions on the Force.com platform.