Press ESC to close

How to SalesForceHow to SalesForce Expert Salesforce Advice for Aspiring Developers, Admins & Beginners.

Salesforce Integration: Basics with Examples

Introduction

Integrating Salesforce with other systems is a crucial aspect of leveraging the full potential of the Salesforce platform. Whether you need to connect Salesforce with another CRM, ERP, or a custom-built application, understanding the fundamentals of Salesforce integration is essential. This blog will guide you through the basics of Salesforce integration with examples and code snippets to help you get started. 

If you wanna become a salesforce developer or already salesforce developer, you need to read my article about What Do Salesforce Developers Do.

What is Salesforce Integration?

Salesforce integration involves connecting Salesforce with other systems or applications to exchange data and automate processes. The goal is to ensure seamless data flow and maintain data consistency across different platforms. Integrations can be done in various ways, including APIs, middleware, or even manually with import/export tools.

Types of Salesforce Integration

  1. API Integration: Using Salesforce’s REST or SOAP APIs to interact with other systems programmatically.
  2. Middleware Integration: Leveraging middleware tools like MuleSoft, Dell Boomi, or Jitterbit to facilitate complex integrations.
  3. AppExchange Products: Using pre-built connectors and integration tools available on the Salesforce AppExchange.
  4. Custom Code Integration: Writing custom Apex code to handle specific integration requirements.

Getting Started with API Integration

API integration is one of the most common methods for connecting Salesforce with other systems. Salesforce provides both REST and SOAP APIs for different use cases. Below is a simple example of how to use Salesforce’s REST API to integrate with an external system.

Example: Integrating Salesforce with an External REST API Using Postman

Step 1: Set Up a Connected App

  1. Go to Setup in Salesforce.
  2. Search for App Manager and click New Connected App.
  3. Fill in the necessary details and enable OAuth Settings. Provide a Callback URL and select the required OAuth Scopes.
  4. Save the Connected App and make a note of the Consumer Key and Consumer Secret.

Step 2: Authenticate and Get an Access Token

Use Postman to authenticate and get an access token.

    1. Open Postman.
    2. Create a new request.
    3. Set the request type to POST.
    4. Enter the URL: https://login.salesforce.com/services/oauth2/token.
    5. In the Body tab, select x-www-form-urlencoded.
    6. Enter the following parameters:
      1. grant_type: password
      2. client_id: YOUR_CONSUMER_KEY
      3. client_secret: YOUR_CONSUMER_SECRET
      4. username: YOUR_SALESFORCE_USERNAME
      5. password: YOUR_SALESFORCE_PASSWORD
    7. Click Send.
    8. Copy the access_token and instance_url from the response.­

Step 3: Perform CRUD Operations

Use the access token to perform CRUD operations. Here’s an example of creating a new account in Salesforce using Postman.

  1. Create a new request in Postman.
  2. Set the request type to POST.
  3. Enter the URL: YOUR_INSTANCE_URL/services/data/v50.0/sobjects/Account/.
  4. In the Headers tab, add:
    • Authorization: Bearer YOUR_ACCESS_TOKEN
    • Content-Type: application/json
  5. In the Body tab, select raw and set the format to JSON.
  6. Enter the following JSON data:
json {    "Name": "New Account"}
  1. Click Send.
  2. Check the response to confirm that the account was created successfully.

Middleware Integration

Middleware tools like MuleSoft, Dell Boomi, or Jitterbit can simplify integration by providing pre-built connectors and visual mapping tools. These platforms support complex integration scenarios and offer robust error handling, data transformation, and process orchestration capabilities.

Example: Using MuleSoft for Integration

  1. Set Up MuleSoft: Sign up for a MuleSoft account and create a new application.
  2. Use Connectors: Use Salesforce and external system connectors to create integration flows.
  3. Configure Data Mapping: Map data fields between Salesforce and the external system using MuleSoft’s visual interface.
  4. Deploy and Test: Deploy the integration and test the data flow between systems.

Custom Code Integration

For specific use cases, you might need to write custom Apex code. Apex provides classes like HttpRequest and HttpResponse for making HTTP callouts to external services.

Example: Making an HTTP Callout in Apex

Let’s take a real-time API available from OpenWeatherMap, which provides weather data.

Step 1: Get API Key

Sign up at OpenWeatherMap and get your API key.

Step 2: Write Apex Code to Make HTTP Callout

Create an Apex class to make an HTTP GET request to the OpenWeatherMap API and handle the response.

apex code
public class WeatherServiceIntegration {
public void getWeather(String city) {
HttpRequest req = new HttpRequest();
String apiKey = 'YOUR_API_KEY';
String endpoint = 'http://api.openweathermap.org/data/2.5/weather?q=' + city + '&appid=' + apiKey;
req.setEndpoint(endpoint);
req.setMethod('GET');
Http http = new Http();
HttpResponse res = http.send(req);

if (res.getStatusCode() == 200) {
// Process the response
String responseBody = res.getBody();
System.debug(responseBody);

// Parse JSON response and extract needed information
Map<String, Object> result = (Map<String, Object>) JSON.deserializeUntyped(responseBody);
String weatherDescription = ((List<Map<String, Object>>) result.get('weather'))[0].get('description');
Decimal temperature = (Decimal) ((Map<String, Object>) result.get('main')).get('temp');

System.debug('Weather: ' + weatherDescription);
System.debug('Temperature: ' + temperature);
} else {
System.debug('Error: ' + res.getStatus());
}
}
}

Step 3: Test the Apex Code

  1. Use Anonymous Apex to test your callout.

apex code:

WeatherServiceIntegration service = new WeatherServiceIntegration();service.getWeather('London');

Conclusion

Salesforce integration is a powerful way to enhance your organization’s productivity and data consistency. By leveraging API integration, middleware tools, or custom code, you can connect Salesforce with virtually any system. This guide provided an overview of the basics with examples and code snippets to help you start your integration journey.

Maaz Ahmed Ansari

Passionate Salesforce Application Architect @ PixelEdge with 3+ years of experience developing triggers, classes, and components and integrating Salesforce with other platforms.