Introduction
Integrating MuleSoft with Salesforce (SFDC) allows seamless data exchange between your applications. To ensure secure authentication, using OAuth 2.0 is the recommended approach. This guide will walk you through the entire process—from setting up a Connected App in Salesforce to configuring MuleSoft with OAuth 2.0 authentication.
Let’s get started!
Prerequisites
Before you begin, ensure you have the following:
Salesforce:
Developer Edition or Enterprise Edition org.
Admin privileges to create a Connected App.
MuleSoft:
Anypoint Platform account.
Anypoint Studio installed with Mule 4 runtime.
Basic knowledge of REST APIs and OAuth 2.0.
Tools:
Postman (optional) for testing the integration.
Step 1: Create a Connected App in Salesforce
To enable OAuth authentication, you need to create a Connected App in Salesforce.
1.1: Log in to Salesforce
Go to Salesforce and log in with admin credentials.
1.2: Navigate to App Manager
In the Setup menu, search for and select App Manager.
1.3: Create a New Connected App
Click “Create an External Client App”.
Fill in the following:
App Name: Choose a descriptive name (e.g., MuleSFDCIntegration).
API Name: Auto-populated.
Contact Email: Enter your email.
Click Create.
1.4: Enable OAuth Settings
Go to the Policies tab.
Select OAuth as the Start Page.
Click on the Settings tab.
Check the box for Enable OAuth Settings.
1.5: Set the Callback URL
Add the Callback URL, which Salesforce will redirect to after authentication.
For local testing, use http://localhost:8081/callback
. For production, add your MuleSoft app’s callback URL.
1.6: Add OAuth Scopes
Choose the necessary scopes:
Access and manage your data (api)
Perform requests at any time (refresh_token, offline_access)
Full access (for initial testing)
Click Save.
1.7: Go to Policies Tab for OAuth Policies
Permitted Users: Admin-approved users are pre-authorized
OAuth Start URL:https://[your-salesforce-domain].my.salesforce.com/services/oauth2/authorize
Select Profiles: System Administrator
1.8: Get Consumer Credentials
Go to Manage Consumer Details.
Copy the:
Consumer Key
Consumer Secret
You’ll use these credentials in MuleSoft.
1.9: App Authorization
Refresh Token Policy: Expire refresh token after specific time.
Step 2: Set Up the OAuth 2.0 Connection in MuleSoft
Next, you’ll configure MuleSoft to use the Salesforce Connected App.
2.1: Create a New Mule Project
Open Anypoint Studio.
Go to File > New > Mule Project.
Name your project.
2.2: Add the Salesforce Connector
Go to Anypoint Exchange.
Search for and add the Salesforce Connector to your project.
2.3: Configure the Salesforce Connector
Drag and drop the Salesforce Config component onto the canvas.
Set the Connection type to OAuth 2.0 Authorization Code Grant.
Fill in the following details:
Authorization URL:https://[your-salesforce-domain].my.salesforce.com/services/oauth2/authorize
Access Token URL:https://[your-salesforce-domain].my.salesforce.com/services/oauth2/token
Client ID: Paste the Consumer Key from Salesforce.
Client Secret: Paste the Consumer Secret.
Callback URL: Use the same Callback URL configured in Salesforce.
Scopes: Use the same OAuth scopes selected in Salesforce.
Step 3: OAuth 2.0 Authorization Flow
Now, let’s understand how the OAuth Dance works:
User Authorization: The Mule app redirects the user to Salesforce’s Authorization URL.
Grant Access: The user logs in and authorizes the connection.
Redirect with Code: Salesforce redirects the user to the Callback URL with an Authorization Code.
Token Exchange: MuleSoft integration solutions exchange the Authorization Code for an Access Token and a Refresh Token.
Token Usage: The Mule app uses the Access Token to make authenticated requests.
Step 4: Token Management in MuleSoft
MuleSoft’s Salesforce Connector automatically handles token generation and refresh. Here’s how it works:
After the initial authorization, MuleSoft receives an Access Token and a Refresh Token.
The Refresh Token is used to obtain new Access Tokens when they expire.
For advanced handling, you can store the tokens in Object Store for reuse.
Step 5: Create a Sample Account in Salesforce
Let’s create a test record in Salesforce using MuleSoft.
5.1: Add the Salesforce Create Operation
Drag and drop the Salesforce Create operation into your flow.
5.2: Configure the Operation
Select the Account object.
Add a Transform Message component before the Create operation.
Use the following payload: { "Name": "MuleSoft Account", "BillingCity": "Anytown", "BillingCountry": "USA" }
5.3: Add an HTTP Listener
Add an HTTP Listener to trigger the flow.
Set the Path to /create.
5.4: Deploy and Test
Deploy your Mule application.
Use Postman or your browser to access:http://localhost:8081/create
Verify that the new account is created in Salesforce.
Conclusion
By following this guide, you’ve successfully:
Created a Connected App in Salesforce.
Configured MuleSoft with OAuth 2.0.
Set up a sample flow to create records in Salesforce.
OAuth 2.0 ensures secure authentication while allowing MuleSoft to interact with Salesforce effectively. You can now extend this setup to perform other Salesforce operations like querying, updating, or deleting records.
Tip: Always test your flow in a development environment before deploying it to production.
Unlock the full potential of MuleSoft with expert integration support. Reach out to us to explore how ProwessSoft can help you achieve seamless connectivity and drive business growth.