Integrating Salesforce with Mule 4 using the Composite Connector allows for efficient, batched operations against Salesforce APIs, reducing network calls and improving performance. This setup is ideal for scenarios where multiple CRUD operations need to be executed in a single transaction, such as during complex data migrations, bulk updates, or synchronized integrations. Before starting, ensure you have appropriate Salesforce API permissions, MuleSoft Anypoint Studio installed, and a configured Salesforce Developer or Production environment with OAuth credentials. This guide walks you through setting up the Composite Connector, provides practical operation examples, discusses limits, and offers troubleshooting tips to help you succeed.
What You’ll Need
- MuleSoft Anypoint Studio installed (version 7.9 or later recommended)
- Salesforce account with API access and OAuth credentials (client ID, client secret, username, password, security token)
- Salesforce connected app configured for OAuth 2.0
- Basic knowledge of Mule 4 flows and DataWeave
- Salesforce Composite API endpoint URL (usually https://yourInstance.salesforce.com/services/data/vXX.X/composite)
Step-by-Step
- Create a Salesforce Connected App: Log into Salesforce, navigate to Setup > Apps > App Manager, and create a new connected app. Enable OAuth settings, select the necessary OAuth scopes (e.g., full access), and save. Note the Client ID and Client Secret.
- Configure OAuth in Mule: In Anypoint Studio, create a new Global Element for OAuth 2.0 configuration. Enter your client ID, client secret, username, password, and security token. Set token URL to Salesforce OAuth token endpoint (https://login.salesforce.com/services/oauth2/token).
- Add the Salesforce Connector: Drag the Salesforce connector into your flow. Choose the OAuth configuration you created, and test the connection to ensure proper setup.
- Configure the Composite API Request: Use an HTTP Request component or the Salesforce connector with the ‘Composite’ operation. Set the method to POST and URL to Salesforce’s Composite API endpoint.
- Create the Composite Request Body: Use DataWeave to craft a JSON payload with multiple sub-requests. For example, include create, update, or delete actions for different Salesforce objects, each identified with a reference ID for response mapping.
- Send the Request and Handle Responses: Send the composite request. Mule will parse the batch responses, allowing you to handle each result separately—logging successes, errors, or triggering subsequent processes based on outcomes.
- Test and Validate: Run your flow with test data. Monitor logs and Salesforce responses to verify batch operations are executed correctly. Adjust the payload or configuration as needed.
- Implement Error Handling: Use Mule’s error handling strategies to catch exceptions, timeouts, or partial failures. Log detailed error messages for troubleshooting.
Common Mistakes
- Using incorrect OAuth credentials or not refreshing tokens before expiration.
- Exceeding Salesforce API limits—batch sizes over 25 requests per composite batch are not supported.
- Incorrect JSON payload structure—missing required fields or improper nesting causes failures.
- Not handling partial failures—assuming all requests succeed without checking response statuses.
- Forgetting to set the correct API version in the endpoint URL, leading to compatibility issues.
Tips
- Always test with small batch sizes first to understand limits and response behaviors.
- Use Mule’s DataWeave transformations to dynamically generate composite requests based on data inputs.
- Leverage response references for handling individual request outcomes efficiently.
- Monitor Salesforce API usage regularly via Salesforce Setup to avoid hitting limits.
- Implement retries with exponential backoff for transient errors, especially in high-volume scenarios.
Troubleshooting
If your composite connector setup does not work as expected, follow these steps:
- Check OAuth Authentication: Verify your OAuth tokens are valid and refresh them if expired. Use the Mule OAuth 2.0 token management features to automate this process.
- Inspect API Limits: Salesforce enforces limits on API calls and batch sizes. Ensure your requests do not exceed these limits (e.g., 25 sub-requests per composite request).
- Validate Payload Structure: Use JSON validators and sample requests from Salesforce documentation. Confirm your payload aligns with Salesforce Composite API specifications.
- Review Error Responses: Salesforce responses include detailed error messages. Log and analyze these to identify issues like invalid object IDs, missing fields, or permission errors.
- Enable Debug Logging: Increase logging levels in Mule to capture detailed request and response data, aiding in pinpointing issues.
- Test with Simplified Requests: Start with a single create or update request to confirm connectivity, then scale up to batch requests.
- Check Network and Endpoint URLs: Ensure the Salesforce API URL is correct and accessible from your Mule environment.
Frequently Asked Questions
1. What is the maximum batch size in Salesforce Composite API?
Salesforce limits composite batch requests to 25 sub-requests per batch. Exceeding this will result in errors, so always keep your batch size within this limit.
2. How do I handle partial failures in composite requests?
Salesforce returns individual status codes for each sub-request. Use Mule’s response handling to check each response’s success or failure and implement logic to retry or log errors accordingly.
3. Can I use the composite connector for delete operations?
Yes, the Salesforce Composite API supports delete requests. You can include delete sub-requests in your batch payload, referencing object IDs to remove records efficiently.
4. What are common causes for authentication failures?
Incorrect OAuth credentials, expired tokens, or misconfigured connected apps often cause auth failures. Ensure tokens are refreshed regularly and credentials are correct.
5. How do I monitor API usage and avoid hitting limits?
Use Salesforce Setup’s API Usage dashboard to monitor your consumption. In Mule, implement logic to track request counts and implement throttling if needed.
6. Is there a way to debug composite requests in Mule?
Yes, enable detailed logging for HTTP requests and responses in Mule. Use tools like Postman or curl to manually test payloads against Salesforce before deploying in Mule.
By following these steps, best practices, and troubleshooting tips, you can effectively utilize the Salesforce Composite Connector in Mule 4, ensuring robust and efficient integrations that leverage Salesforce’s powerful batching capabilities.