Salesforce Integrations

Introduction

Salesforce allows other apps to be integrated with the Salesforce platform. It results in data communication between systems. We can make a callout to external services in Salesforce using Apex which is Salesforce’s proprietary language.

What is an API?

API is called Application Programming Interface. It is designed for software that allows applications to talk to each other

What is Salesforce Integration?

Salesforce Integration is the process of integrating or merging the functionality of Salesforce with other applications or systems, which gives users a unified view.

With the help of Apex callouts, we can integrate our Apex code with an external web service.

Apex REST Callouts

Since Rest callouts are based on HTTP, each request has an HTTP method and an endpoint.

HTTP methods tell us what type of action is taking place.

Below are some HTTP Methods

  1. GET – used to retrieve the resource information
  2. POST – used to create the data
  3. PUT – used to create new or update an existing resource
  4. DELETE – used to delete the data/resource

Rest service sends requests in JSON format, which is nothing but a string. So, there is a class JSONParser that converts the JSON into an object.

Whenever a request is processed by the server, we get a status code in the response, which tells us about our request status, if it is processed successfully or not. There are different status codes for each type.

Example

Before writing code for GET method, we need to authorize the endpoint URL of the callout through Authorize Endpoint Addresses.

Http http = new Http();
HttpRequest rqt = new HttpRequest();
rqt.setEndpoint(‘https://randomurl.com/places’);
rqt.setMethod(‘GET’);
HttpResponse res = http.send(rqt);
If(res.getStatusCode() == 200){
//deserialize the JSON and print the result
}

 

We can expose our Class as a REST Service by defining the class as global and defining methods as global and static keywords.

Syntax

@RestResource(urlMapping = ‘/Contact/*’)
global with sharing class MyRestClass{
@HttpMethod
global static returnType getRecord(){
//code
}
}

 

SOAP Callouts

Apex makes callouts to SOAP using XML, which is mainly used in legacy systems.

Salesforce provides us with a tool called WSDL2Apex which automatically generates Apex classes with the help of the WSDL documents.

We can expose our Class as a SOAP Service by defining the class as global and adding the keyword ‘webservice’ with a ‘static’ modifier to each method we want to expose.

Syntax

global with sharing class MySOAPClass{
webservice static returnType getRecord(String Id){
//code
}
}

 

 

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.