API Integration in Salesforce
API Integration in Salesforce
1. Introduction
SOAP API
Description: SOAP API allows for robust and secure integration between Salesforce and other
enterprise applications. It supports transaction control and has built-in retry logic.
Use Cases: Ideal for integrations requiring strong security and complex transactions.
Example:
https://www.linkedin.com/in/iam-murali/
API Integration in Salesforce
REST API
Description: REST API is lightweight and easy to use, making it suitable for mobile and web
applications. It uses standard HTTP methods (GET, POST, PUT, DELETE).
Use Cases: Ideal for building simple and quick integrations, especially for mobile and web apps.
Example:
// REST API integration
HttpRequest request = new HttpRequest();
request.setEndpoint('https://instance.salesforce.com/services/data
/v52.0/sobjects/Account');
request.setMethod('POST');
request.setHeader('Authorization', 'Bearer ' + sessionId);
request.setHeader('Content-Type', 'application/json');
request.setBody('{"Name":"New Account"}');
HttpResponse response = http.send(request);
Bulk API
Description: Bulk API is designed to handle large data volumes asynchronously. It allows for the
processing of large datasets in a single API call.
Use Cases: Ideal for data migrations, data loads, and other bulk data operations.
Example:
// Bulk API integration example
HttpRequest request = new HttpRequest();
request.setEndpoint('https://instance.salesforce.com/services/async
/52.0/job');
request.setMethod('POST');
request.setHeader('Authorization', 'Bearer ' + sessionId);
request.setHeader('Content-Type', 'application/xml');
request.setBody('<?xml version="1.0" encoding="UTF-8"?><jobInfo
xmlns="http://www.force.com/2009/06/asyncapi/dataload"><operation>i
nsert</operation><object>Account</object></jobInfo>');
HttpResponse response = http.send(request);
https://www.linkedin.com/in/iam-murali/
API Integration in Salesforce
Metadata API
Description: Metadata API is used to manage Salesforce metadata, such as custom object
definitions, page layouts, and more.
Use Cases: Ideal for deploying changes between Salesforce environments, managing customization,
and building deployment tools.
Example:
// Metadata API integration example
MetadataService.MetadataPort service = new
MetadataService.MetadataPort();
service.SessionHeader = new
MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = sessionId;
MetadataService.CustomObject customObject = new
MetadataService.CustomObject();
customObject.fullName = 'CustomObject__c';
customObject.label = 'Custom Object';
customObject.pluralLabel = 'Custom Objects';
customObject.nameField = new MetadataService.CustomField();
customObject.nameField.type_x = 'Text';
customObject.nameField.label = 'Name';
MetadataService.AsyncResult[] results = service.create(new
MetadataService.Metadata[] { customObject });
Auth Provider
What is an Auth Provider?
An Auth Provider in Salesforce allows you to authenticate users via third-party services using
OAuth 2.0.
https://www.linkedin.com/in/iam-murali/
API Integration in Salesforce
Auth Endpoint URL: The URL to which users are directed to authenticate.
Token Endpoint URL: The URL to obtain the access token.
User Info Endpoint URL: The URL to fetch user information.
Example: Setting up OAuth with Google
Google Developer Console: Create a new project, enable the Google+ API, and create OAuth
credentials.
Salesforce Setup: Create an Auth Provider using the Google credentials.
Configuration: Use the Client ID and Client Secret from Google, set the Authorize Endpoint
URL to https://accounts.google.com/o/oauth2/auth, Token Endpoint URL to
https://accounts.google.com/o/oauth2/token, and Default Scopes to openid
profile email.
Connected App
What is a Connected App?
A Connected App allows external applications to integrate with Salesforce using APIs.
https://www.linkedin.com/in/iam-murali/
API Integration in Salesforce
API Terminology
Endpoint: The specific URL where an API is accessible (e.g.,
https://api.example.com/data).
Request and Response: The communication between the client and server. A request is sent
by the client, and a response is returned by the server.
Authentication vs. Authorization: Authentication verifies the identity of a user or system.
Authorization determines what resources the authenticated user or system can access.
Tokens (Access Token, Refresh Token): Tokens are used for securing API requests. An
access token is used for accessing resources, while a refresh token is used to obtain a new
access token when the current one expires.
Headers: Additional information sent with an API request or response (e.g., Content-Type:
application/json).
JSON and XML: Data formats used in API communication. JSON (JavaScript Object Notation)
is lightweight and easy to read, while XML (eXtensible Markup Language) is more verbose and
used for complex data structures.
Rate Limits and Quotas: Restrictions on the number of API calls that can be made within a
specific time period to prevent abuse and ensure fair usage.
https://www.linkedin.com/in/iam-murali/
API Integration in Salesforce
201 Created: The request has been fulfilled and resulted in a new resource being created.
Example:
request.setEndpoint('https://api.example.com/records');
request.setMethod('POST');
request.setBody('{"name":"New Record"}');
HttpResponse response = http.send(request);
if (response.getStatusCode() == 201) {
// Record created successfully
}
400 Bad Request: The server could not understand the request due to invalid syntax.
Example:
HttpResponse response = http.send(request);
if (response.getStatusCode() == 400) {
// Handle Bad Request
}
401 Unauthorized: The client must authenticate itself to get the requested response.
Example:
HttpResponse response = http.send(request);
if (response.getStatusCode() == 401) {
// Handle Unauthorized
}
403 Forbidden: The client does not have access rights to the content.
Example:
HttpResponse response = http.send(request);
if (response.getStatusCode() == 403) {
// Handle Forbidden
}
404 Not Found: The server can not find the requested resource.
Example:
HttpResponse response = http.send(request);
if (response.getStatusCode() == 404) {
// Handle Not Found
}
https://www.linkedin.com/in/iam-murali/
API Integration in Salesforce
500 Internal Server Error: The server has encountered a situation it doesn't know how to
handle.
Example:
HttpResponse response = http.send(request);
if (response.getStatusCode() == 500) {
// Handle Internal Server Error
}
Example:
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.example.com/data');
request.setMethod('GET');
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
String responseBody = response.getBody();
// Process the response
} else {
// Handle errors
}
Example:
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://jsonplaceholder.typicode.com/posts');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
request.setBody('{"title":"foo","body":"bar","userId":1}');
HttpResponse response = http.send(request);
if (response.getStatusCode() == 201) {
String responseBody = response.getBody();
// Process the response
} else {
// Handle errors
}
DELETE Example:
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://jsonplaceholder.typicode.com/posts/1');
request.setMethod('DELETE');
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
// Record deleted successfully
} else {
// Handle errors
}
https://www.linkedin.com/in/iam-murali/
API Integration in Salesforce
} else {
// Handle errors
}
https://www.linkedin.com/in/iam-murali/
API Integration in Salesforce
https://www.linkedin.com/in/iam-murali/
API Integration in Salesforce
https://www.linkedin.com/in/iam-murali/
API Integration in Salesforce
https://www.linkedin.com/in/iam-murali/
API Integration in Salesforce
https://www.linkedin.com/in/iam-murali/
API Integration in Salesforce
https://www.linkedin.com/in/iam-murali/
API Integration in Salesforce
// Usage
List<String> accountIds = new List<String>{'001xxxxxxxxxxxx',
'002xxxxxxxxxxxx'};
System.enqueueJob(new AccountUpdateQueueable(accountIds));
https://www.linkedin.com/in/iam-murali/
API Integration in Salesforce
Security Considerations:
Optimizing Performance:
Caching responses
Cache responses where possible to reduce redundant API calls and improve performance.
https://www.linkedin.com/in/iam-murali/
API Integration in Salesforce
// Usage
String key = 'https://api.example.com/data';
String cachedResponse = ApiCache.getCachedResponse(key);
if (cachedResponse == null) {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(key);
request.setMethod('GET');
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
cachedResponse = response.getBody();
ApiCache.cacheResponse(key, cachedResponse);
}
}
https://www.linkedin.com/in/iam-murali/