Integration/Web Services in Salesforce
Integration in general refers to the communication of one system to another. This inter system communication is achieved by API (Application Program Interface). APIs enable a system to expose their own functionality or consume the services of another third party system.
Integration in salesforce is achievable in two ways :
- REST API
- SOAP API
REST APIs, also known as RESTful web services are based on representational state transfer (REST), which is a protocol followed in API development to facilitate communication between two systems.
REST API works with Http headers to send along information or receive information from outside systems. It is a module based system where each functionality is defined by its own REST endpoint and method.
The following are the methods used in REST APIs :
- GET : Used to reading a resource
- POST : Used send new data to a resource
- PUT : Used to update a resource
- PATCH : Also used to update a resource, but can support partial updates
- DELETE : Delete a resource from a Salesforce server
NOTE : Currently Salesforce does not support calling out to external systems with the PATCH method. The PATCH method is not supported in apex.
REST can work with both the JSON and XML data languages. And it is a lightweight protocol which doesn’t use too much bandwidth.
SOAP or Simple Objects Access Protocol is a web communication protocol used to define APIs by using xml files.
The SOAP API only works with xml files. As compared to REST, SOAP does not keep the functionality modular, instead SOAP follows a more generalized approach of keeping all the functionality together in one module.
SOAP APIs use WSDL files to expose/consume all the services together in a single file and then provide this WSDL file to the other system for consumption. Therefore, SOAP is a more heavyweight framework as compared to REST APIs.
SOAP APIs in Salesforce work with WSDL files only to generate apex classes from those WSDL files, which let you call the external service from inside salesforce.
REST vs SOAP
|REST stands for Representational State Transfer.||SOAP stands for Simple Object Access Protocol.|
|REST works for a multitude of data formats like XML,JSON, text etc.||SOAP only works with XML format.|
|REST works with an endpoint and an http method to expose the system.||SOAP works with WSDL files to expose the system.|
|REST is a more lightweight system.||SOAP is a more heavyweight system.|
|REST requires less bandwidth to use.||SOAP requires more bandwidth to use.|
Remote Site Settings
Before we can make callouts from salesforce to external systems, we need to add the URL of those systems to the Remote Site Settings in salesforce.
This lets the salesforce system know that this is a secure URL which is safe to callout to, if we don’t add the URL to remote site settings we will see an error like this we make the callouts :
Unauthorized endpoint, please check Setup->Security->Remote site settings.
Add the URL to the remote site settings by going to Setup->Security->Remote Site Settings and creating a new record.
Click on New Remote Site and enter the url, Salesforce will only save the base URL of the endpoint and allow all requests to that base URL to go through from salesforce to the external system.
NOTE : This is applicable for both REST and SOAP callouts.