How to Debug Apex Errors in Salesforce
Overview
Apex is the programming language used in Salesforce to create custom business logic. Like any code, Apex can run into errors. Debugging these errors effectively is key to maintaining a reliable and scalable Salesforce environment.
This article walks you through the steps and best practices to debug Apex errors using Salesforce’s built-in tools.
🔍 Common Apex Error Types
Before debugging, it helps to understand the most frequent Apex error types:
Error Type | Description |
---|---|
NullPointerException | Occurs when accessing a variable that hasn’t been initialized. |
DmlException | Errors related to data manipulation operations like insert , update , etc. |
QueryException | Issues with SOQL queries (e.g., too many rows, no rows). |
LimitException | Hits Salesforce governor limits (CPU time, SOQL queries, etc.). |
UnhandledException | Catch-all for errors not handled by try-catch blocks. |
🧰 Tools for Debugging Apex
1. Debug Logs
- Go to: Setup → Debug Logs → New
- Set the user you want to capture logs for and reproduce the issue.
- View logs in the Developer Console or download them.
- Filter levels: Set log levels to
FINE
orFINER
for Apex Code and System for deeper inspection.
2. Developer Console
- Open via: User Menu → Developer Console
- Use:
- Logs Tab: View detailed logs for each transaction.
- Checkpoints: Set breakpoints to inspect variable states.
- Execution Stack: Step through methods to trace errors.
3. Apex Exception Email
- Salesforce can automatically send exception emails when unhandled errors occur.
- Enable in: Setup → Apex Exception Email
4. System.debug() Statements
- Add
System.debug()
to your code to print variable values. - Check the output in the Debug Logs.
5. Try-Catch Blocks
- Use structured error handling to isolate and manage errors:
try {
// Your logic here
} catch (Exception e) {
System.debug(‘Error: ‘ + e.getMessage());
}
🧪 Best Practices for Debugging
- Reproduce the Error: Always try to reproduce the issue in a sandbox or developer org.
- Minimize Log Size: Use
System.debug()
selectively; avoid over-logging. - Use Custom Exceptions: Create specific exceptions to better categorize issues.
- Governor Limit Checks: Be mindful of limits using
Limits.getLimit*()
methods. - Review Test Methods: Run unit tests with debug logs to identify failures and trace logic.
💡 Pro Tip
Use UserInfo.getUserId()
or logging context-specific info to trace errors caused by different users or profiles.
🧾 Example Debug Log Snippet
USER_DEBUG [10]|DEBUG|Start Insert
EXCEPTION_THROWN [12]|System.DmlException: Insert failed. First exception…
This shows:
- Line of code:
[10]
- Log category:
DEBUG
- Your debug message:
Start Insert
- Followed by the exact error message on line 12.
✅ Summary
Debugging Apex errors requires a mix of tools and best practices. Start with logs, use the Developer Console, and handle exceptions thoughtfully. With structured debugging, you can identify root causes quickly and maintain a more reliable Salesforce org.