Skip to main content
Build the future with Agentforce at TDX in San Francisco or on Salesforce+ on March 5–6. Register now.

Implement Frameworks

Learning Objectives

After completing this unit, you’ll be able to:

  • Describe what a framework is.
  • Explain the advantages of using frameworks.
  • Create and use a trigger framework.
  • Describe other types of frameworks that support Salesforce development.

What Is a Framework?

A framework is a highly optimized, reusable structure that serves as a building block. These building blocks provide common functionality that developers can override or specialize for their own needs. Reusable frameworks increase the speed of development, improve the clarity and efficiency of your code, and simplify code reviews and debugging.

Yep, frameworks are going to be your new best friends.

If you are not familiar with Apex triggers or can use a refresher, we suggest that you review the Apex Triggers module before continuing.

Trigger Frameworks

Triggers are a powerful tool that can do great things when used correctly but cause a lot of headache when used incorrectly. Triggers without structure can be messy. They can interfere with one another and cause huge performance and debugging problems.

A good trigger framework is:

  • Scalable, due to built-in bulkification.
  • Traceable, clearly showing what logic is executed, and when, for each record.
  • Reusable, because functional methods and classes are clearly separated.
  • Atomic, allowing you to bypass specific triggers when needed.
  • Optimized, making minimal Salesforce Object Query Language (SOQL) calls, preventing recursion, and sharing result sets over the execution cycle.

By now you're probably excited about trigger frameworks and beginning to wonder which framework is best. 

As with many good things in life—ice cream, pizza toppings, Salesforce mascots—it’s impossible to choose a single favorite. Each trigger framework has its pros and cons. Which framework you use depends on your situation. Having said that, Kevin O'Hara's SFDC Trigger Framework is the one we generally prefer on large-scale projects within the Success Cloud team at Salesforce. (For more information about the SFDC Trigger Framework, see the Resources section.) The SFDC Trigger Framework has an abstract TriggerHandler base class that handles execution and prevents recursion. Child classes inherit from the TriggerHandler class, and override one or more of the trigger context methods (like beforeInsert, beforeUpdate, or afterUpdate). The framework is simple to use and scales well. Inspect the framework and consider how you can use it in your future projects.

Other Frameworks

While trigger frameworks are the basis of sound Salesforce development, there are many other types of frameworks that make your life as a developer much easier. We encourage you to learn more about them, but would be remiss not to mention the following types.

  • Error logging frameworks: A well-developed Salesforce org isn't one that never runs into errors, it's one that knows how to record them. Logging errors in a consistent, structured way supports debugging during the build phase and after launch. Your framework should write debug entries to a custom object. From there you can create workflow rules to send email notifications, create tasks, or create a ticket in an external system, when an error event occurs.
  • Bypass patterns: Configurations like validation rules, workflow rules, triggers, and process builders are necessary, but sometimes they get in your way (when you're loading large amounts of data or trying to debug a pesky error, for example). Bypass patterns use hierarchical custom settings or custom permissions to temporarily disable these configurations on the level of a user, profile, or org. Because both hierarchical custom settings and custom permissions are supported in formula fields, you can read their values in your validation rules, triggers, and process builders.
  • Test data factory: Good unit tests start with good data. Reusable methods for setting up test data speeds up development and increases the reliability of your tests. To learn more about the concept of test data factories, see the Create Test Data for Apex Tests unit in the Apex Testing module.

Now that you are familiar with frameworks and understand their uses, let’s look at some Salesforce coding best practices.

Resources

Share your Trailhead feedback over on Salesforce Help.

We'd love to hear about your experience with Trailhead - you can now access the new feedback form anytime from the Salesforce Help site.

Learn More Continue to Share Feedback