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.

  • Published 23 Apr 2024
  • 2 mins read
How to use external services in Apex
Table of contents
Article Highlights
  • The article discusses how external services in Apex simplify the integration with external systems by providing a standardized way to interact with external APIs, eliminating the need for manual boilerplate code1.
  • It highlights the rapid development capabilities in Salesforce, where developers can import an external service’s OpenAPI specification to generate Apex classes and methods, enabling easy invocation of the service2.
  • The generated Apex classes from external services are reusable, promoting code reusability and reducing redundancy

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

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 their 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,” which 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.

Last updated: 11 Jul 2024