Integrating Salesforce APEX REST
In this post, walk through the process of invoking an APEX REST method in MuleSoft and fetching account information from Salesforce using APEX code snippets.
Join the DZone community and get the full member experience.
Join For FreeIn the realm of modern enterprise integration, the harmonious interaction between different systems and platforms is paramount. Salesforce, being one of the leading CRM platforms, often necessitates seamless integration with other systems to leverage its data and functionalities. MuleSoft, on the other hand, is a powerful integration platform that facilitates connecting various applications and APIs.
One common scenario in Salesforce integration is invoking APEX REST methods from MuleSoft to interact with Salesforce data. In this blog post, we'll walk through the process of invoking an APEX REST method in MuleSoft and fetching account information from Salesforce using APEX code snippets.
Why Use APEX REST Methods?
Salesforce APEX REST methods provide a flexible way to expose custom functionalities or access Salesforce data through RESTful APIs. Leveraging APEX REST methods allows for fine-grained control over what data is exposed and how it's accessed, making it a preferred choice for integrating Salesforce with external systems like MuleSoft.
Prerequisites
Before we dive into the integration process, ensure you have the following prerequisites in place:
- Salesforce developer account: You'll need a Salesforce Developer account to create and test APEX REST methods.
- MuleSoft Anypoint platform account: Sign up for MuleSoft Anypoint Platform to create Mule applications.
Integration Steps
1. Create APEX REST Method in Salesforce
First, let's create an APEX REST method in Salesforce to fetch account information. Here's a simple example:
apex
@RestResource(urlMapping='/accountInfo/*')
global with sharing class AccountInfoRestController {
@HttpGet
global static Account getAccountInfoById() {
RestRequest req = RestContext.request;
String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
// Fetch Account information by Id
Account acc = [SELECT Id, Name, Industry, Phone, BillingCity FROM Account WHERE Id=:accountId];
return acc;
}
}
In this code snippet, we define an APEX class AccountInfoRestController
annotated with @RestResource
to expose a REST endpoint /accountInfo/
. The getAccountInfoById()
method retrieves account information based on the provided Account ID.
2. Deploy APEX Class in Salesforce
Deploy the AccountInfoRestController
class to your Salesforce organization.
3. Create MuleSoft Application
Create a new MuleSoft application in Anypoint Studio.
File --> New --> Mule Project
4. Configure HTTP Connector
Add an HTTP Listener to your Mule flow to receive incoming HTTP requests. Configure the listener with the appropriate host and port.
5. Invoke the APEX REST Method in MuleSoft
There are 2 ways to invoke the APEX REST method in MuleSoft.
5.1. Using HTTP Requester
Add an HTTP Request component after the HTTP Listener and configure it to make a GET request to the Salesforce APEX REST endpoint:
- URL: Specify the URL of the APEX REST method in the format
https://<Salesforce_Instance_URL>/services/apexrest/accountInfo/<Account_Id>
. - Method: Set the method to
GET
. - Headers: Include any required headers, such as authentication tokens.
5.2. Using Invoke APEX Rest API Connector
Add an Invoke APEX Rest API Connector after the HTTP listener and configure it to make a GET
request to the Salesforce APEX REST endpoint:
- APEX class: Specify the name of the class
AccountInfoRestController
- APEX method: This is the combination of 4 attributes:
methodName^urlMapping^httpMethod^returnType
,getAccountInfoById^/accountInfo^HttpGet^Account
- Salesforce configuration:
Basic Authentication
- Username: Your login user ID
- Password: Your login password
- Security Token: Generate from
Profile --> Settings --> Reset My Security Token
6. Parse Response
After invoking the APEX REST method, parse the response body to extract account information. Use transform message to send payload in required structure to calling flow.
{
"Id": "xxxxxxxxxxxxxxx",
"Name": "Example Account",
"Industry": "Technology",
"Phone": "(555) 555-5555",
"BillingCity": "San Francisco"
}
7. Handle Errors and Transform Data
Implement error handling and data transformation as per your application requirements.
8. Complete the Mule Flow
Complete the Mule flow with the necessary components for further processing or response generation.
Conclusion
Integrating Salesforce APEX REST methods in MuleSoft enables seamless interaction between Salesforce and other systems. By following the steps outlined in this guide, you can effectively invoke APEX REST methods from MuleSoft and retrieve account information from Salesforce. This integration approach empowers organizations to unlock the full potential of their Salesforce data within their broader application ecosystem.
Opinions expressed by DZone contributors are their own.
Comments