[SalesForce] Consume SFDC REST Service through Mulesoft ESB

I wanna consume a REST service exposed by Salesforce, right now I'm using a Sandbox instance and using Anypoint Studio to create my flow, I would like to know what's the best practice to implement it. Now I'm not sure about the salesforce connector allows me to consume the service, so, I'm using this components:

enter image description here

This is whole flow.

<mule xmlns:sfdc="http://www.mulesoft.org/schema/mule/sfdc" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:oauth2="http://www.mulesoft.org/schema/mule/oauth2" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" version="CE-3.6.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/sfdc http://www.mulesoft.org/schema/mule/sfdc/current/mule-sfdc.xsd">
    <http:request-config name="HTTP_Request_Configuration" host="https://test.salesforce.com" port="80"  doc:name="HTTP Request Configuration" protocol="HTTPS">
        <oauth2:client-credentials-grant-type clientId="3xQYwWqsvZUYbwMRLHiXQy9A23XXXtxg1OMxdXh5p4eoev53q26CmN0Yx0EyLf_HTLW9X" clientSecret="710XXX1723685311">
            <oauth2:token-request tokenUrl="https://test.salesforce.com/services/oauth2/token"/>
        </oauth2:client-credentials-grant-type>
    </http:request-config>
    <file:connector name="File" writeToDirectory="/Users/emoran/Desktop" autoDelete="true" streaming="true" validateConnections="true" doc:name="File"/>
    <flow name="outbrain_3wintegrationFlow1">
        <poll doc:name="Poll">
            <fixed-frequency-scheduler frequency="10" timeUnit="SECONDS"/>
            <http:request config-ref="HTTP_Request_Configuration" path="services/apexrest/outbrain_integration_messages/" method="GET" requestStreamingMode="ALWAYS" sendBodyMode="ALWAYS"  doc:name="Consume SFDC LOG Service" doc:description="prueba" parseResponse="false" source="#[message.payload]" target="#[message.payload]">
                <http:request-builder>
                </http:request-builder>
                <http:success-status-code-validator values="0..599"/>
            </http:request>
        </poll>
        <logger message="&gt;&gt;&gt;&gt; RESPONSE FROM SERVER:  #[message.payload]" level="DEBUG" doc:name="Logger"/>
        <echo-component doc:name="Echo"/>
        <file:outbound-endpoint path="/" outputPattern="refacciones.json" connector-ref="File" responseTimeout="10000" doc:name="File"/>
    </flow>
</mule>

What would be the best approach to solve this?

Thanks

Best Answer

Its easy to integrate Salesforce and Mule. Mule integrates with Salesforce without the need of worrying which mechanism to use - example, Mule can integrate easily with XML APIs of Salesforce and as a Mule developer, you dont need to worry about preparing XMLs. The Salesforce connector does it all for you :)

1) The first step is to install Salesforce connector in your Anypoint studio. To do that, go to Help Menu --> Install new software --> select Anypoint Connectors Suit and Standard (based on your license) --> Salesforce Connector and click Next, accept terms & conditions and Install. 2) To consume a service from Salesforce, you need Salesforce user name, password and security token (which can be generated in Salesforce). Please create an user for Mule to query Salesforce 3) Create a new Salesforce Connector using 'Global Elements' tab in your flow. Provide your Salesforce user name, password and security token. Refer to img1_salesforce_connector.png file.
enter image description here (it is recommended to use a property placeholder to read them from property files so that you externalize these) 4) Create the flow (based on yours), remove the echo and file component. Add the Salesforce Endpoint. To query salesforce for accounts, select the salesforce endpoint in the flow, open the 'Mule propeties view' and select the Query in the drop down. Refer to image img2_Salesforce_Query.png. enter image description here 5) To query Salesforce accounts, place your SOQL in Salesforce endoint. Refer to image img3_getAccounts.png enter image description here 6) You are nearly there - Add a logger component and print the payload #[payload]. You can add data mapper and a database insert Salesforce output to DB directly :D Refer to image img4_datamapper_db.png enter image description here You are done!

You can also refer to Template projects given by Mulesoft in Anypoint studio. To get different sample projects, click on the gear icon below the File Menu. Select an example template: Salesforce to Database.

Have a nice day with Mule ESB!!

Related Topic