How to use External Services in Apex?
External Services has been a part of the Salesforce platform for some time now. The External Services product team has made significant improvements in recent releases to enhance its adoption and overall usability. One notable enhancement is the ability for Salesforce Developers to reference External Services in Apex, which can be incredibly advantageous.
Let’s first take a look how to reference an External Service in Apex.
Consider, there is an external service registered in your org named PayPal which allows you to fetch a list of invoices with a method called getInvoices. So the way we call this method in Apex is as follows.
ExternalService.PayPal api = new ExternalService.PayPal();// add a new Request objectExternalService.BankWebService.getInvoices_Request request = new ExternalService.BankWebService.getInvoices_Request();/* * You can set request params if, there are any. * For example, * request.pageSize = 10; */ExternalService.BankWebService.getInvoices_Response response = api.getInvoices(request);
But you might ask, why would you want to use this as a Salesforce developer.
Benefits of External Services
Using External Services in Apex offers several advantages:
No more boiler plate code: External Services provide a standardized approach to connect and interact with external APIs, simplifying the integration process. This eliminates the need for the boiler plate for handling HTTP requests, authentication, and response parsing.
Accelerated development: With External Services, you can swiftly create integrations by importing the OpenAPI specification of the external service. Salesforce automatically generates Apex classes and methods based on the API specification, enabling easy invocation of the external service.
Code reusability: External Services generate reusable Apex classes that encapsulate the API operations of the external service. These generated classes can be utilized across various sections of your Apex codebase, promoting code reusability and reducing redundancy.
Enhanced maintainability: When there are changes to the API specification of the external service, you can update the External Service definition in Salesforce. This triggers the regeneration of Apex classes, ensuring that your code stays synchronized with the latest API changes. This simplifies the maintenance process and keeps your integrations up to date.
Flow compatibility: External Services can also be directly called via Flows, making them accessible for administrators and consultants who work with Salesforce without extensive coding knowledge.
Hope you find this one useful! Catch you in the next one! ✌ And thank you for being an awesome reader! Subscribe to this blog for receiving all the latest updates straight to your inbox.

.png)




Comments