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
- API Integration: Using Salesforce’s REST or SOAP APIs to interact with other systems programmatically.
- Middleware Integration: Leveraging middleware tools like MuleSoft, Dell Boomi, or Jitterbit to facilitate complex integrations.
- AppExchange Products: Using pre-built connectors and integration tools available on the Salesforce AppExchange.
- 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
- Go to Setup in Salesforce.
- Search for App Manager and click New Connected App.
- Fill in the necessary details and enable OAuth Settings. Provide a Callback URL and select the required OAuth Scopes.
- 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.
-
- Open Postman.
- Create a new request.
- Set the request type to POST.
- Enter the URL: https://login.salesforce.com/services/oauth2/token.
- In the Body tab, select x-www-form-urlencoded.
- Enter the following parameters:
- grant_type: password
- client_id: YOUR_CONSUMER_KEY
- client_secret: YOUR_CONSUMER_SECRET
- username: YOUR_SALESFORCE_USERNAME
- password: YOUR_SALESFORCE_PASSWORD
- Click Send.
- 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.
- Create a new request in Postman.
- Set the request type to POST.
- Enter the URL: YOUR_INSTANCE_URL/services/data/v50.0/sobjects/Account/.
- In the Headers tab, add:
- Authorization: Bearer YOUR_ACCESS_TOKEN
- Content-Type: application/json
- In the Body tab, select raw and set the format to JSON.
- Enter the following JSON data:
json { "Name": "New Account"}
- Click Send.
- 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
- Set Up MuleSoft: Sign up for a MuleSoft account and create a new application.
- Use Connectors: Use Salesforce and external system connectors to create integration flows.
- Configure Data Mapping: Map data fields between Salesforce and the external system using MuleSoft’s visual interface.
- 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
- 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.