How To Use External Services In Apex

External services have been a Salesforce platform feature for a while now. Over the past few releases, the Salesforce team has significantly improved its adoption and overall usability.

How To Use External Services In Apex
Table of contents

One such feature is the ability to reference external services in Apex, which can be very useful for Salesforce Developers.

Let's Find Out Why

The Advantages Of Using External Services In Apex

Simplified integration

External services abstract the complexity of integrating with external systems by providing a standardized way to define and interact with external APIs. It eliminates the need to manually write boilerplate code for handling HTTP request and response requests, authentication logic, and parsing responses.

Rapid development

You can quickly create integrations by importing the external service's OpenAPI (formerly known as Swagger) specification. Salesforce generates Apex classes and methods based on the API specification, allowing you to invoke the external service easily.

Code reusability

External services generate reusable Apex classes that encapsulate the external service's API operations. These generated classes can be used across multiple parts of your Apex codebase, promoting code reusability and reducing redundancy.

Improved maintainability

When the API specification of the external service changes, you can update the external service definition in Salesforce, regenerate the Apex classes, and ensure that your code is synchronized with the latest API changes. This simplifies the maintenance process and keeps your integrations up to date.

Call via Flow

External services can also be called directly via Salesforce Flow, making them usable for Salesforce Admins and Consultants.

Calling External Services From Apex

Say, you registered an external service named “OpenLibrary,” and this service allows you to get a list of books using the method “getBooks().”

For example, let’s get a list of books using Apex.
 ExternalService.OpenLibrary api = new ExternalService.OpenLibrary();
 // add a new Request object
 ExternalService.OpenLibrary.getBooks_Request request = new  ExternalService.OpenLibrary.getBooks_Request();
  * You can set request params if there are any.
  * For example,
  * request.pageSize = 10;
 ExternalService.OpenLibrary.getBooks_Response response = api.getBooks(request);

Simplify The Complexity Of External Services Integration

As you can see, external services in Apex offer Salesforce developers a powerful tool for integrating with external systems and APIs. 

By leveraging this feature, developers can:

  • Simplify integration complexity
  • Accelerate development cycles
  • Promote code reusability
  • Improve the maintainability of their applications.
Moreover, external services empower Salesforce Administrators and Consultants by enabling them to directly call external services via Flow, expanding the range of use cases where external services can be leveraged within the Salesforce platform.