Update salesforce objects and listen to the salesforce objects via WSO2 Micro Integrator

Ramindu De Silva
9 min readFeb 19, 2022

WSO2 Micro Integrator is capable of integrating with Salesforce in few ways. In this article, we use Salesforce’s REST API to updates it records and Salesforce’s Push Topics to trigger integrations in micro integrator whenever a subscribed object category is changed in Salesforce.

As the 1st scenario, we will be using the Micro Integrator to update salesforce objects. And in the second scenario, we will use the 1st scenario’s integration logic to do the changes to salesforce object which will result in salesforce object changes which will capture by the WSO2 Micro Integrator’s 2nd Integration. Following is the flow diagram

Prerequisites: Salesforce Access Token Generation

Configuring Managed Application in Salesforce

First we need to get access tokens to invoke the Salesforce REST API.

First step is to get a developer account by signing up in https://developer.salesforce.com/signup

After the verification email comes in login to the salesforce with the given URL in the email. eg: https://wso22-dev-ed.my.salesforce.com

Log in to Salesforce after verifying your account, with the newly created credentials. In the upper-right corner, select Setup.

Navigate to Apps > App Manager and click on the New Connected App.

Fill in Connected App Name (SF_Connector), API Name (SF_Connector), and Contact Email(wso2user2022@gmail.com) under Basic Information.

Scroll down to the API (Enable OAuth Settings) and

  • Enable OAuth Settings
  • Add https://login.salesforce.com as the Callback URL
  • Select Manage user data via APIs, Manage user data via Web, Perform requests at any time

Scroll down and save the connected application.

From the left panel, click on “Manage Connected Apps” and click on “Edit”

Under OAuth policies, check whether “All users may self-authorize” in the “Permitted Users list” is selected and if not, please select.

Now we can obtain the access token and refresh token as indicated below.

Enter the following URL in your web browser. Highlighted values should be replaced with your instance name and the consumer key.

https://<INSTANCE>.salesforce.com/services/oauth2/authorize?response_type=code&client_id=<CONSUMER_KEY>&redirect_uri=https://login.salesforce.com
  • INSTANCE: “Instance Name” in my example it is “UM8”. You can get it by navigating to company information from the left toolbar
  • CONSUMER_KEY: Navigate to app manager and view your application. and under API(Enable OAuth Settings), you could see the “Consumer Key”

Once you execute with the correct values and if you have already logged in the browser, Allow access for the pop up that you get.

And you will get an URL similar to https://login.salesforce.com/?code=aPrxCM8Qk1CjfMDeYdaLXnFW_BkM4s8FMKj53Uk562PTL5ILjUHfRWDbG2Q5ETK._VE9cN08Uw%3D%3D and note down the highlighted value

And execute the following cURL and get the needed tokens. Replace <INSTANCE>, <CODE>, <CONSUMER_KEY>, <CONSUMER_SECRET>

And you should get a access token and a request token

Scenario 1: Creating the Integration Project to Update Salesforce Objects

Download necessary connectors

Go to EI Connectors | WSO2 Store and search for “Salesforce” and click on “SalesforceRest”

Download both connector and the inbound endpoint

  • Copy org.apache.synapse.salesforce.poll.class-2.0.1.jar file into <ProductHome>/lib folder.
  • Connector will be added to the integration project we create in the following stage

Create integration to connect with Salesforce REST API

Create new integration project

Right click on the project and create a “connector exporter”. In the wizard opens, give a name (SFConnectorExporter) and click on finish

Right click on the created “Connector Exporter” and click on “Add or remove connectors”

Select on “Add connector/Module” in the opened wizard and select “file system” and browse and select the downloaded salesforcerest-connector-2.1.0.zip. Click on “finish” when done.

Create a new REST API and configure salesforce connectors

Give the following values in the wizard and click on finish

Click on the resource and enable “POST” method, since we are sending a payload to this service to create an Account. And also add a URL-Template to the resource as well.

Goto the design view and drag and drop the “create” connector into the in sequence section

Click on the dropped artefact, click on the design view and add a new salesforce connection. Here you will have to give the values that you already have.

Now, we need to access the payload that we send to Micro Integrator s that we could send it to salesforce. Since we are sending a JSON object we will have to extract the values using JSON patths. In order to to that, In the same design view, enter sObject Name as “json-eval($.sObject)” and Field and Value as “json-eval($.fieldAndValue)”.

Note: in order to enter the values, you need to click on the “function” icon and select “JSON” and enter the given values as below

Just to check whether we have successfully added the Salesforce object, wi can query from the Salesforce REST API, In order to do that we could use the Query connector in WSO2 Micro Integrator. Now drag and drop the query connector.

In the design view of the query connector, now you can just select the previously created “salesforce_connection” and add “select id, name from Account” as the “Query String” value

Now add a respond mediator to get the respond back to the client. Since we got queried all the accounts in the salesforce from the query connector, If our operations are successful, the invoked client should get a list of accounts including the account we newly added.

If you click on the source view of the integration, it should look as follows.

Export the integration to Micro Integrator

As a final step, we need to export the integration to the micro integrator. Right click on the composite exporter and click on “Export Composite Application Project”

In the opened wizard, give the export destination as the absolute destination to the carbonapps in the micro integrator as shown below.

Select all the necessary artefacts that we created and click on finish.

Testing the scenario

Do a POST request to “http://localhost:8290/salesforcerest/create” with the following payload.

And you will be getting a response as follows which contains the Account that you just added as well.

Scenario 1: Create integration to listen to with Salesforce Objects

Setting up the PushTopic in Salesforce

Login to the Salesforce Account. Navigate to the top right corner of the Home page and click the Setup icon. Then select Developer Console.

After populating the Developer console, click Debug -> Open Execute Anonymous Window.

Add the following entry in the Enter Apex Code window and click Execute.

Login https://workbench.developerforce.com/login.php, you will only be able to login to the workbench only if you have signed in with a developer account.

Select the “Query” dropdown and select “Streaming Push Topics”

Select the push topic as “Account” and click on “Subscribe”

Testing the PushTopic Channel

Open a new window and login to the workbench again. Select “data” dropdown, select “insert”, select the “Object type” as “Account” and click on “next

In the opened form, just enter a test name and click on “confirm” at the bottom of the form.

In the “Streaming Push Topics” browser window. You’ll see a your new Account details.

Reset Security Token

Once you click on “Reset Security Token” the token will be emailed to the email you are registered with.

When setting up the Inbound Endpoint you need to provide the Salesforce password as “salesforce login password”+”security token from the email”

Now since the subscription is working, and got the security token, lets configure the integration which listens to the “Account” salesforce object.

Configuring inbound endpoint to listen to the salesforce object

For an Inbound endpoint, we need to provide a sequence to send the message in. So lets add a new sequence via the integration studio.

After selecting the “sequence”, select “Create new sequence”, give a name “salesforceInboundEPsequence” for the sequence and click “Finish”.

Then lets just add a simple log to the sequence. Click on the source view of the sequence and replace the text and save. This log will ensure us that an event from the subscribed topic has arrived to Micro Integrator.

Now lets create the inbound endpoint which subscribes to the salesforce object changes and correlate with the sequence we just created.

For to do that, right click on the configs folder and select “Inbound Endpoint”. In the next window gets opened, click on “Create a New Inbound Endpoint” and click on “Next”.

Select “Custom” from the “Inbound Endpoint Creation Type” dropdown and provide the values as follows. Here we map the sequence we created to the inbound endpoint.

After the inbound endpoint is created, switch to the source view and replace the text with the following. You will have to change EventIDStoredFilePath, userName and password (concatenation of the salesforce password and security token)

Note: EventIDStoredFilePath is simply a new file in the local machine which will be used to track the latest arrived event id.

Then save all the files and export the CAPP just as before by selecting the new files that got added.

Testing the scenario

Now lets Do another POST request to “http://localhost:8290/salesforcerest/create” with a payload similar to follows (change the fieldValue.name to a different value. eg: SA_EU2)

Once the request is executed, a log similar to the following should be printed in the micro integrator logs, and the response if the create should received with the list of accounts as before.

[2022-02-18 20:14:37,390]  INFO {LogMediator} - To: , MessageID: urn:uuid:460A228DA9E6CCC1381645195477388, Direction: request, Integration = salesforce listener, Payload: {"event":{"createdDate":"2022-02-18T14:44:36.545Z","replayId":4,"type":"created"},"sobject":{"Id":"0018d000006o1OPAAY","Name":"SA_EU2"}}

Note: When request comes in WSO2 Micro Integrator. will create a salesfornce object and query and send back the list of accounts as response, while the salesforce inbound endpoint will listen to Account object and log the events as above.

I hope you guys got some understanding about the integration capabilities of the WSO2 Micro Integrator with Salesforce via this article and also a brief step by step guide on how to configure.

--

--