Get to Know the Apex Recipes Sample App

Diving In

Congratulations on installing Apex Recipes in your Trailhead playground. Let’s dive in and look at a few of the recipes offered. These recipes illustrate a couple of intelligent abstractions—bits of code that are specific enough to do meaningful work, but generic enough to be reusable. In this unit, you investigate two of these abstractions—making Http callouts to external services, and writing sustainable triggers.

Making Http Callouts

It’s pretty common to need to make an Http callout to a third-party service. Whether that’s to get current currency conversion rates or to update your company's ERP system, making a callout is something developers often do. The Apex code to make a callout is straightforward, but quite boilerplate. An intelligent abstraction on top of the base callout code can result in less code that is easier to test. Apex Recipes contains not only recipes that demonstrate how to make a callout, but code that illustrates how to establish a reusable, intelligent abstraction.

Open your playground org, and open the Apex Recipes app. Here’s how you do that:

  1. From the App Launcher, select Apex Recipes. This opens the Apex Recipes Lightning app.
  2. Apex Recipes are organized into groups. Click the > next to Integration Recipes. 
  3. Click Callout Recipes.

Code viewer for Apex Recipes showing the CalloutRecipes class.

As soon as you click a specific recipe, the code is displayed on the right. But it’s not just the code. You also see tabs at the top for its corresponding Test Class, documentation for the class, and other classes that are related. Callout Recipes, for example, has two additional related classes RestClient and CanTheUser

Scroll down in the CalloutRecipes code tab until you find the method rawCallout(). This recipe walks you through creating the necessary objects, setting the endpoint, and firing off the callout. The other methods in this class make calls to methods like get(), del(), put(), and post(). These methods are provided by the virtual parent class RestClient. Classes that extend RestClient can utilize all the methods provided by RestClient while overriding methods where necessary. Comparing the rawCallout() method to the httpGetCalloutToSecondOrg() method illustrates how this intelligent abstraction helps reduce the amount of code needed. rawCallout() has approximately five lines of code dedicated to making the callout, before processing the response. On the other hand, httpGetCalloutToSecondOrg() accomplishes the same work of making a GET HTTP call with a single line. 

Writing Sustainable Triggers

Triggers are the Swiss Army chainsaw of the Apex developers world—you can do just about anything with them, but if you’re not careful, you can cut down the wrong tree. With that in mind, Apex Recipes follows best practices by utilizing a Trigger Handler Framework as an intelligent abstraction. There are two triggers in Apex Recipes; one on Account, the other on a custom Platform Event. The beautiful thing about the trigger handler Apex Recipes uses is that it works for both use cases! Let’s look at the Account Trigger Handler:

Go ahead and open that class in the UI like this:

  1. Click the > next to Trigger Recipes.
  2. Click the AccountTriggerHandler class.

Apex Recipes UI

Note the two related code tabs that show up on the right: TriggerHandler and AccountServiceLayer. Click the TriggerHandler tab. This is the heart of the Trigger Handler framework abstraction. Scroll down until you find the run() method. This method’s switch statement is responsible for calling the AccountTriggerHandler various context methods. Now let's go back to the main AccountTriggerHandler tab and look at the AccountTriggerHandler class. You’ll find methods in here corresponding to the various trigger contexts. Scroll down until you find the beforeInsert() method. The beforeInsert() method calls code from other classes. In this case our AccountServiceLayer. Click that tab to the right and scroll down until you find the incrementCounterInDescription() method. This method contains the business logic we’re interested in having the trigger execute. Building it this way allows us to have code run during a trigger that is also available for other parts of the org to run. This architecture also puts developers in control of the business logic’s order of operations and makes it easier to test! A win, win, win!

There’s So Much More

You learned about Apex Recipes’ RestClient and TriggerHandler. But there’s so much more here. Throughout the app, there are examples of SOQL queries that respect FLS and CRUD. There’s example code using conditional like if, else, and switch statements. You’ll find examples of Asynchronous Apex including Queueables, Batch, and Scheduled classes. While we intend to add more recipes to this sample app, it is already an excellent reference for the right way to code Apex.

We hope that these recipes help you understand not only how to accomplish specific tasks, but how to write safer, more sustainable code.

We won’t check any of your work in this step. Click Verify Step to finish the project and earn your badge.