Salesforce Apex Integration Services Part 1
Contents
[NOTE] Updated March 14, 2020. This article may have outdated content or subject matter.
Prologue
The Salesforce platform offers different sets of integration capabilities that enable you to tightly integrate your Apex code with an external service. This feat is possible through three features dubbed callouts:
- Apex Rest Callout
- Apex SOAP Callout
- Apex Web Service
Note: Web service callouts to SOAP web services use XML, and typically require a WSDL document for code generation. HTTP callouts to services typically use REST with JSON. Before any Apex callout can invoke an external site, that site must be registered in the Remote Site Settings page, or the callout will fail.
Remote Sites
To able to integrate with external services, each service endpoint must be linked to a remote site in Salesforce. To create a Remote site follow the steps below.
- From Setup, enter Remote Site Settings in the Quick Find box, then click Remote Site Settings.
- Click New Remote Site.
- Enter the site name.
- Enter the site URL.
- Click Save.
Apex REST Callouts
The fundamental concept in any RESTful API is the resource. A resource is an object with a type, associated data, relationships to other resources, and a set of methods that operate on it. A REST request consists of four components: a resource URI, an HTTP method, request headers, and a request body. Each resource can be accessed using standard HTTP methods, for example (HEAD, GET, POST, PATCH, DELETE). Request headers specify metadata for the request. The request body specifies data for the request.
Apex REST Callout Examples
Requirement: Please create a Remote Site with the following URL
https://jsonplaceholder.typicode.com
and nameTestCallout
. Just for testing purposes, delete after.
Get Data from a Service
The following snippet queries the supplied comments endpoint for the post with id = 5, firstly the array of comments are deserialized. we then deserialize each comment only if the response was successful.
|
|
Send Data to a Service
The code snippet below creates a new resource using the following url.
|
|
Let us put it all together in one class called FakePostCallouts
.
|
|
Testing REST Callouts
Apex test methods don’t support callouts, and tests that perform callouts fail. To remedy this plight, the testing runtime allows you to “mock” the callout. Mock callouts allow you to specify the response to return in the test instead of actually calling the web service. You can mock callouts either by implementing the HttpCalloutMock interface or using static resources.
Test a Callout with HttpCalloutMock
This interface enables you to specify the response that’s sent in the respond
method. Your test class instructs the Apex runtime to send this fake response by calling Test.setMock again. The class implementing this interface must be annotated with the @isTest annotation.
|
|
Testing FakePostCalloutsMock using FakePostCalloutsMock requires one important step to be done:
- Call
Test.setMoock(HttpCalloutMock.class, new FakePostCalloutsMock());
top level in your test class
|
|
Apex SOAP Callouts
In addition to REST callouts, Apex can also make callouts to SOAP web services using XML. Soap callouts are contingent on a WSDL document. Web Services Description Language (WSDL) is an XML-based file that basically tells the client application what the web service does. The WSDL file is used to describe in a nutshell what the web service is capable of and provide the client with all the information required to connect to the web service and use all the functionality provided by the web service.
Note: Use outbound messaging to handle integration solutions when possible. Use callouts to third-party web services only when necessary.
Note: WSDL2Apex automatically generates Apex classes from a WSDL document. You download the web service’s WSDL file, and then you upload the WSDL and WSDL2Apex generates the Apex classes for you. The Apex classes construct the SOAP XML, transmit the data, and parse the response XML into Apex objects. Instead of developing the logic to construct and parse the XML of the web service messages, let the Apex classes generated by WSDL2Apex internally handle all that overhead. Code examples wont be provided in this article for Soap Web Services.
Epilogue
The moral from this article was to ease your introduction towards the various Integration services Salesforce has to offer. Rest Callout was discussed, specifically how to retrieve resources from a external service and how to create resources. Various testing examples were also demonstrated, such as Callout mocking and actual apex testing. SOAP Callouts and Platform Integration Services will be subsequently covered in part 2 and 3 in this series. Thanks for stopping by and I hope you were able to learn or reinforce your knowledge regarding Rest Callouts on the Salesforce platform 😃
Author Remario Richards
Modified March 14, 2020