💃 Patience is a Virtue 🕺

Taming Long-Running Callouts with the Continuation Class in Apex

Good morning, Salesforce Nerds! If you've been working with Salesforce long enough, you’ve likely faced the dreaded "limits" wall. 🧱 

Whether it's governor limits or timeouts, sometimes it feels like the platform loves reminding us who’s boss.

One particular challenge developers often face is the 120-second limit for callouts -especially when integrating with external services that take their sweet time responding.

Enter Salesforce's Continuation class: your secret weapon for turning long-running callouts into efficient, asynchronous miracles. 🧞 

Ready to make time wait for you? Let’s dive in!

TABLE OF CONTENTS

UNDERSTANDING WHAT IT IS

WHAT IS IT?

At its core, the Continuation class is like a virtual “hold, please” button for callouts. ☝️ 

It allows you to make asynchronous requests to long-running external services without worrying about the platform's timeout limits.

When the external system finally responds (whether it takes 5 seconds or 5 minutes), Salesforce picks up where it left off and handles the response. 🔥 

No more throwing your hands in the air because your API partner needs to "process something" for more than 120 seconds!

WHAT ARE THE BENEFITS

WHY SHOULD YOU CARE?

🚫 No More Timeout Errors: Normally, Salesforce has a hard stop at 120 seconds for synchronous callouts. The Continuation class allows you to bypass this limit by breaking out of the synchronous waiting game.

😀 User Experience: Long-running processes can leave users sitting and waiting (cue the spinning circle of doom). Continuations allow you to give users immediate feedback ("We’re processing your request!"), so they don’t lose patience—or worse, confidence in your solution.

🛟 Governor Limits Safety: Since the Continuation class is asynchronous, you aren't tied down by some of the constraints that come with synchronous processes, giving you more room to breathe.

📈 Scalable Integrations: In complex enterprise ecosystems where Salesforce plays the role of middleware or orchestration layer, Continuations make it easier to handle intricate integrations without bogging down system performance.

DIFFERENT SCENARIOS YOU CAN USE IT IN

SAMPLE USE CASES

💸 Payment Gateways

You’ve got an integration with a payment processor that takes its sweet time validating transactions. Instead of waiting around for the payment status and hitting that 120-second limit, you can use a Continuation! The callout is made, and when the processor finally comes back with a result (whether it’s approved or denied), Salesforce will pick up and process the payment response asynchronously.

🖥️ Order Fulfillment Systems

Let’s say your company sells custom-built rocket ships (hey, dream big!). Customers order online, and Salesforce kicks off the process to calculate shipping timelines from your manufacturing partner. Sometimes, though, the factory takes a while to confirm availability. With a Continuation, you can submit the request and allow your backend team to calculate those timelines without worrying about Salesforce timeouts.

📊 External Reporting Engines

Imagine you’re integrating with a third-party reporting engine that aggregates data across multiple systems. It’s a resource-heavy job that can take longer than 120 seconds. Using a Continuation, you can asynchronously request the report and notify your users when it’s ready for download. They can go about their day and check back later when the report is done.

LET’S SEE AN EXAMPLE

HOW TO USE IT

Here’s a simplified example (straight from the documentation) of how you might implement the Continuation class in an Apex controller: 🧑‍💻 

public with sharing class SampleContinuationClass {
    // Callout endpoint as a named credential URL
    // or, as shown here, as the long-running service URL
    private static final String LONG_RUNNING_SERVICE_URL =
        '<insert your callout URL here>';
    
    // Action method
    @AuraEnabled(continuation=true cacheable=true)
    public static Object startRequest() {
      // Create continuation. Argument is timeout in seconds.
      Continuation con = new Continuation(40);
      // Set callback method
      con.continuationMethod='processResponse';
      // Set state
      con.state='Hello, World!';
      // Create callout request
      HttpRequest req = new HttpRequest();
      req.setMethod('GET');
      req.setEndpoint(LONG_RUNNING_SERVICE_URL);
      // Add callout request to continuation
      con.addHttpRequest(req);
      // Return the continuation
      return con;
    }
    
    // Callback method
    @AuraEnabled(cacheable=true)
    public static Object processResponse(List<String> labels, Object state) {
      // Get the response by using the unique label
      HttpResponse response = Continuation.getResponse(labels[0]);
      // Set the result variable
      String result = response.getBody();
      return result;
    }
}

WRAPPING IT UP

CONCLUSION

The Continuation class is a game-changer for developers working with long-running external systems. ⌛️ 

With it, you can make Salesforce wait without actually "waiting," improving user experience, preventing timeouts, and building scalable integrations.

Whether you’re processing payments, handling complex fulfillment processes, or running heavy reports, Continuation gives you the tools to make it all happen seamlessly. 🏗️ 

So next time you're facing down the barrel of a timeout, don’t worry - use Continuation and become the time-bending superhero your org deserves!

SOUL FOOD

Today’s Principle

"Have patience. All things are difficult before they become easy.”

Saadi

and now....Salesforce Memes

What did you think about today's newsletter?

Login or Subscribe to participate in polls.