How to SalesForce

Salesforce Integration: Basics with Examples

Salesforce Integration

Introduction

One important part of getting the most out of the Salesforce tool is integrating it with other systems. Understanding the basics of Salesforce connection is important whether you need to link Salesforce to another CRM, ERP, or a custom-built app. This blog will show you the basics of integrating Salesforce and give you examples and bits of code to get you going.

If you want to become a Salesforce developer or are already one, you need to read my article about What Salesforce Developers Do.

What is Salesforce Integration?

Salesforce integration means linking Salesforce to other programs or systems so that data can be shared and tasks can be done automatically. The goal is to make sure that info flows smoothly and stays the same across all devices. There are many ways to integrate things, such as using APIs, software, or even import/export tools to do it by hand.

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

One of the most popular ways to link Salesforce to other systems is through API connection. For different uses, Salesforce gives you both REST and SOAP APIs. Here’s a simple example of how to connect an outside system to Salesforce’s REST API.

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. Please 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

By giving you pre-built connections and visual mapping tools, middleware tools like MuleSoft, Dell Boomi, or Jitterbit can make interaction easier. It is possible for these platforms to handle complicated interaction situations and provide strong error handling, data transformation, and process automation features.

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 a 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

Please sign up at OpenWeatherMap to obtain 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

Integrating Salesforce is a powerful way to make your business more productive and ensure that all of its data is correct. You can connect Salesforce to almost any system using API connection, bridging tools, or your own code. This guide gave you a review of the basics, along with some examples and code to get you started on your integration path.

Picture of Maaz Ahmed Ansari

Maaz Ahmed Ansari

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

Recent Posts

Tags

[post_tags]