- Salesforce Integration Guide/Notes
- Reference
- It's needed when we need to connect 2 or more different systems e.g.; if you wanted to show your dropbox files on your salesforce org page.
-
Fast
-
Supports all types of devices.
-
Works with JSON, XML, and other formats.
-
Slow (compared to REST).
-
Works with XML.
-
It's more secure in comparison to REST.
-
It's mostly used in Enterprise Applications.
-
You get a document called
WSDL
, which has all the information about requests and responses of any given service. It helps you create request and response classes and methods with a parser (in most cases). In the case of REST, you don't have any such facility.
-
GET - Retrieve the records.
-
POST - Insert the records.
-
PUT - Insert new records or update existing records.
-
PATCH - Update existing records.
-
Delete - Delete existing records.
// Since we are making calls over http, we need an instance of HTTP.
Http http = new Http();
// We also need an instance of HttpRequest, which we will send over http using HTTP instance.
HttpRequest request = new HttpRequest();
// Now we ware adding the information related to request e.g.; Endpoint we are hitting and type of request it's. In this case it's GET.
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
request.setMethod('GET');
// Here we make a request and expect a response in HttpResponse instance.
HttpResponse response = http.send(request);
// If the request is successful (status code is 200), parse the JSON response.
if(response.getStatusCode() == 200) {
// Deserialize the JSON string into collections of primitive data types.
Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
// Cast the values in the 'animals' key as a list
List<Object> animals = (List<Object>) results.get('animals');
System.debug('Received the following animals:');
for(Object animal: animals) {
System.debug(animal);
}
}
- You can run the above code in the developer console. Go to the developer console, and from there, click on
Debug
->Open Execute Anonymous Window
.
- In
Anonymous Window
, you can execute the code by clicking theExecute
button at the bottom of the screen
- In Salesforce, before making any request to an external API, we need to authorize it. Otherwise, we will get the following error.
System.CalloutException: Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint = https://api.spoonacular.com/recipes/random
- Now to make a request, you need to add your site in Remote site settings. You can go there by visiting
Setup->Security->Remote Site Settings
-
A WSDL is an XML document that describes a web service.
-
It talks about the functions that you can implement or expose to the client e.g.; getRecipe etc., request parameters, which are optional, which are required, etc., and also about the response.
-
It actually stands for Web Services Description/Definition Language.
- SOAP is an XML-based protocol that lets you exchange info over a particular protocol (can be HTTP or SMTP, for example) between applications.
- It stands for Simple Object Access Protocol and uses XML for its messaging format to relay the information.
WSDL: When we go to a restaurant, we see the Menu Items, those are the WSDLs.
Proxy Classes: Now after seeing the Menu Items we make up our minds. So, basically, we make Proxy classes based on WSDL Document.
SOAP: Then when we actually order the food based on the Menus. Meaning we use proxy classes to call upon the service methods which are done using SOAP.
You can create Apex classes using the WSDL
file but it's no different than making a request to REST base call-in terms of code. In the body of the request, you will have to send XML in SOAP protocol, and in response, you should expect XML in SOAP protocol.
- Go to
Setup -> Apex Classes -> Generate from WSDL
- Select the WSDL file and click
Parse WSDL
- Now you will show the classes extracted from the WSDL file. You get the opportunity to change the name of classes before classes are created.
- For each generated class, a second class is created with the same name and the prefix
Async
. TheCalculatorServices
class is for synchronous callouts. TheAsyncCalculatorServices
class is for asynchronous callouts.
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals.asmx');
req.setHeader('Content-Type', 'text/xml; charset=UTF-8');
req.setHeader('SOAPAction', 'SomeAction');
req.setHeader('Content-Length', '0');
req.setBody('<?xml version="1.0" encoding="utf-8"?>' +
+ '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'
+ '<soap:Body xmlns="">'
+ '</soap:Body></soap:Envelope>');
System.debug('Request =' + req.getBody().unescapeXml());
Http http = new Http();
HttpResponse res = http.send(req);
- You can expose your Apex class methods as a
REST or SOAP
web service operation. - By making your methods callable through the web, external applications can integrate with Salesforce.
-
You have an internal app used to make calls to potential leads.
-
You can show the lead data from Salesforce in your app and also get updates on the lead status if any.
- Define your class as
global
. - Define methods as
global static
. - Add annotations to the class (@RestResource(urlMapping='/Account/*')) and methods (@HttpGet)
@RestResource(urlMapping='/Account/*')
global with sharing class MyRestResource {
@HttpGet
global static Account getRecord() {
// Add your code
}
}
-
The base endpoint for Apex REST is https://yourInstance.my.salesforce.com/services/apexrest/.
-
The URL mapping is appended to the base endpoint to form the endpoint for your REST service (https://yourInstance.my.salesforce.com/services/apexrest/Account/*)
-
The URL mapping is case-sensitive and can contain a wildcard character (*).
The following method annotations are available.
- Define your class as
global
. - Add the
webservice
keyword and thestatic
definition modifier to each method you want to expose. - The
webservice
keyword provides global access to the method it is added to.
global with sharing class MySOAPWebService {
webservice static Account getRecord(String id) {
// Add your code
}
}
-
The external application can call your custom Apex methods as web service operations by consuming the class WSDL file.
-
You can generate this WSDL for your class from the class detail page, accessed from the Apex Classes page in Setup.
- OpenAPI: An API for which you don't need to authorize yourself to make the call. Spoonacular is one such example.
- Request
- Response
- Statuscode
- Endpoint
- Callout
- Trailhead | Apex Integration Services
- Trailhead | Data Integration Specialist
- APEX + Integration
- SFDCFacts Academy | Salesforce Integration Crash Course | The Ultimate Guide to Salesforce Integrations | In 100 Minutes
- Integration and Apex Utilities
- Apex Rest
- REST API Developer Guide
- APIs and Integration
- SFDC Panther+ | Salesforce Integration Tutorials
- Salesforce Hulk | Understanding REST APIs and Integrations Basics in Salesforce Development