Creating a Seamless Integration: A Step-by-Step Guide to Building a Salesforce Apex API and Connecting it with MuleSoft In today's fast-paced business environment, seamless integration is key to boosting productivity and driving growth. One way to achieve this is by building a Salesforce Apex API and connecting it with MuleSoft. In this tutorial, we will show you how to design a sample Apex API that retrieves account names and phone numbers from Salesforce, and then create a MuleSoft REST API to access the Apex API and return the response payload. To get started, follow these steps: * Log in to your Salesforce developer account using your username and password. * Click on the Setup option located at the top right-hand corner of the page. * In the left-hand side menu, navigate to the Build category and select Develop - Apex Classes. * Click on New and enter the following code, then click Save: @RestResource(urlMapping='/showAccountsDetails') global class checkAccount { @HttpGet global static LIST getAccount() { LIST lst; try { lst = [select name,phone from Account]; return lst; } catch(Exception ex) { system.debug('Error'+ex.getMessage()); } return lst; } } By following these steps, you can unlock seamless integration and boost productivity with Salesforce Apex API and MuleSoft connectivity solutions. To learn more about how to achieve this, visit carsnewstoday.com.
Step 1: Initialize Anypoint Studio
Commence by launching Anypoint Studio, which will serve as our development environment for this project.
Step 2: Establish a New Mule Project
Next, navigate to File New Mule Project to initiate the project creation process.
Step 3: Define Project Parameters
Now, enter "test-apex-API" as the Project Name and click Finish. This will create the project with the specified designation.
Step 4: Expand the Project and Modify pom.xml
After clicking Finish, the project will be created. Expand the project and open the pom.xml file for editing.
Step 5: Integrate Salesforce Connector Dependency
Under the dependencies tag in the pom.xml file, incorporate the following dependency:
dependency groupIdcom.mulesoft.connectors/groupId artifactIdmule-salesforce-connector/artifactId version10.1.0/version classifiermule-plugin/classifier/dependency
This dependency is essential for the integration of the Salesforce Apex API with Mulesoft.
Important Reminder: Ensure you are utilizing Mule version 9.9.1 or later, as previous versions encountered a session token error that has now been resolved.
Establishing the Mule Flow
To start, open the test-apex-api.xml file.
Step 1: Incorporate the HTTP Listener
Locate the HTTP listener in the Mule Palette and drag it onto the test-apex-api.xml Message Flow tab.
Step 2: Set Up the Listener
Select the listener, and a new tab will appear below. Click the plus icon next to the Connector configuration, input the necessary details as illustrated in the screenshot, and click OK.
Step 3: Define the Path
In the General tab, input the path “/test apex”.
Step 4: Insert the Logger
Find the logger in the Mule Palette, drag it in, and set the logger message to “Test apex API flow initiated”.
Step 5: Modify the Message
Search for the Transform Message in the Mule Palette, drag it after the logger, and set the payload to an empty JSON.
Step 6: Call the Apex REST Method
Look for the Invoke apex rest method in the Mule Palette, drag it after the Transform Message, and configure the apex connector by clicking the plus icon next to “connector configuration”. Enter your Salesforce configuration details as shown in the screenshot.
Step 7: Set Up the Apex Connector
If you lack the security token for your developer account, you can create one by accessing your Salesforce developer account, navigating to My Settings – Personal – Reset My Security Token, and following the instructions.
Step 8: Verify the Connection
After entering the Salesforce details, verify your connection by clicking on Test Connection. Once the test connection is successful, click OK on the test connection window and then click OK on the Salesforce Config Window.
Step 9: Configure the Invoke Apex REST Method
In the “Invoke apex rest method” connector, navigate to “General” under “Apex class definition” and input the following details:
* Apex Class Name: checkAccount
* Apex Class Method Name: getAccount (getAccount^/showAccountsDetails^HttpGet^ListAccount^)
Note: The Apex Class Name and Apex Class Method Name will be automatically populated by DataSense. If DataSense is not functioning, verify the connector configuration details again and test the connection. Then, click the refresh option next to the Apex Class Name.
Step 10: Transmute the Response Payload
Insert an additional Transform Message to transmogrify the response payload emanating from the apex API into JSON format. Drag and drop the Transform Message from the Mule Palette subsequent to the “Invoke apex rest method” connector and incorporate the requisite code.
Step 11: Integrate the Logger
Integrate the logger to print the response. Drag and drop the “Logger” from the Mule Palette subsequent to the Transform Message connector and configure it as depicted in the screenshot.
Step 12: Execute the Mule Project
Execute the Mule project.
Step 13: Test the API
Upon executing the Mule project, open Postman and issue a GET call to http://localhost:8081/testapex.Understanding the Response Output
When you execute the code, you can anticipate seeing the output displayed as shown below.
Key Takeaway: Output Storage
It is crucial to note that the response output is stored against a specific key, which is generated by appending “Output” to the method name. For instance, if you utilize the method “getAccount”, the output will be stored against the key “getAccountOutput”.