Best Procatices for Apex Triggers and Apex classes in Salesforce

Himanshu Varshney
Senior Salesforce Developer
December 21, 2023

NestPracticeApex

Apex Triggers:

Bulkify Your Code:

Write your triggers to handle bulk operations to avoid hitting governor limits.

Avoid using SOQL and DML statements inside loops.


Use Trigger Context Variables:

Leverage Trigger context variables (e.g., Trigger.new, Trigger.old, Trigger.newMap, Trigger.oldMap) to access and manipulate records effectively.


Keep Triggers Logic Simple:

Keep trigger logic simple and delegate complex logic to handler classes.

Follow the Single Responsibility Principle.


Avoid Hardcoding IDs:

Avoid hardcoding IDs and query for records when needed.

Use custom settings or custom metadata types for configuration.


Error Handling and Logging:

Implement proper error handling and logging.

Use try-catch blocks to handle exceptions and log them appropriately.


Avoid Recursive Triggers:

Use static variables or trigger handler patterns to prevent recursive trigger execution.

Consider using a framework like Trigger Framework to manage trigger execution.


Governor Limits:

Be mindful of Salesforce governor limits.

Use aggregate queries instead of row-level queries when possible.


Test Coverage:

Ensure your triggers have sufficient test coverage (ideally, 100%).

Cover positive and negative scenarios, bulk scenarios, and edge cases.


Use Asynchronous Processing:

Move time-consuming or non-critical logic to future methods or queueable classes to keep triggers efficient.


Apex Classes:

Use Descriptive Names:

Use meaningful and descriptive names for classes, methods, and variables.

Follow a consistent naming convention.


Follow Design Patterns:

Use design patterns like Singleton, Factory, and Strategy when applicable.

Implement the Separation of Concerns and Dependency Injection principles.


Governor Limits:

Be aware of governor limits and design your classes to avoid hitting these limits.

Consider using batch processing for large data sets.


Avoid SOQL Queries in Loops:

Avoid using SOQL queries inside loops to prevent hitting query limits.

Use collections and bulk patterns.


Exception Handling:

Implement proper exception handling to provide meaningful error messages.

Use custom exceptions when needed.


Code Comments and Documentation:

Include comments for complex code sections.

Document your classes and methods using ApexDoc or other documentation conventions.


Reuse Code:

Promote code reuse by creating utility classes and methods.

Modularize your code for better maintainability.


Test Coverage and Unit Tests:

Maintain high test coverage for your classes.

Write unit tests that cover various scenarios, including positive and negative cases.


Avoid Hardcoding:

Avoid hardcoding values; use custom settings or custom metadata types for configuration.


Security Considerations:

Follow Salesforce security best practices, such as CRUD and FLS checks.

Use the "with sharing" or "without sharing" keywords appropriately based on your security requirements.

By following these best practices, you can write more robust and efficient Apex Triggers and Classes in Salesforce, contributing to a more maintainable and scalable application.

Share this article:
View all articles

Related Articles

Choosing the Right Data Sources for Training AI Chatbots featured image
December 12, 2025
If your AI chatbot sounds generic, gives wrong answers, or feels unreliable, the problem is probably not the model. It is the data behind it. In this article, you will see why choosing the right data sources matters more than any tool or framework. We walk through what data your chatbot should actually learn from, which sources help it sound accurate and confident, which ones quietly break performance, and how to use your existing knowledge without creating constant maintenance work. If you want a chatbot that truly reflects how your business works, this is where you need to start.
Lead Qualification Made Easy with AI Voice Assistants featured image
December 11, 2025
If your sales team is spending hours chasing leads that never convert, this is for you. Most businesses do not have a lead problem, they have a qualification problem. In this article, you will see how AI voice assistants handle the first conversation, ask the right questions, and surface only the leads worth your team’s time. You will learn how voice AI actually works, where it fits into real sales workflows, and why companies using it respond faster, close more deals, and stop wasting effort on unqualified prospects. If you want your leads filtered before they ever reach sales, keep reading.
The Automation Impact on Response Time and Conversions Is Bigger Than Most Businesses Realize featured image
December 9, 2025
This blog explains how response time has become one of the strongest predictors of conversions and why most businesses lose revenue not from poor marketing, but from slow follow up. It highlights how automation eliminates the delays that humans cannot avoid, ensuring immediate engagement across chat, voice, and form submissions. The post shows how automated systems capture intent at its peak, create consistent customer experiences, and significantly increase conversion rates by closing the gap between inquiry and response. Automation does not just improve speed. It transforms how the entire pipeline operates.

Unlock the Full Power of AI-Driven Transformation

Schedule a Demo

See how Anablock can automate and scale your business with AI.

Book Now

Start a Voice Call

Talk directly with our AI experts and get real-time guidance.

Call Now

Send us a Message

Summarize this page content with AI