Hosting an app in Heroku and connecting with salesforce and then fetching data from AWS Mqsql (RDS) using mulesoft in realtime
Hello Friends,
#Most important thing — A demo is attached at bottom of this blog ..YES! You can first see the output and if you like that please go thru this blog to get the details of how it is being done.
Hope you are all doing well and finding new ways to connect between salesforce and different systems (as we all do 😁 ).
This blog is categorized in 2 parts , first part is related to how can we host a app in Heroku which is using Heroku postgresql database to store data and that table is in bidirectional sync with Salesforce , that means the data entered via Heroku app will be using and updating directly in Salesforce table.
And Second part is about connecting Salesforce data and AWS RDS data by the integration platform Mulesoft — fun part is this integration happens in real time , yes no more http callout or polling of the data , totally event based.
Technical tools involved in this blog are:
i)Heroku
ii)Postgresql
iii)Pgadmin
iv)Salesforce streaming topic
v)Github
vi)Mulesoft
vii)AWS RDS
viii)Mysql workbench
Hope both these part will be a helpful reference for your future work.
Part I (Connecting Salesforce with heroku)

Here we are going to build an app written in node.js and hosted in Heroku , and the database used is Heroku postgresql .The page we built looks like below , user can enter the details in the following page , the details would be first stored in Postgresql database and in turn would be sent to Salesforce to create a new Account record.

Step by step illustration :
i) First we need to create an heroku App and connect the add ons heroku connect and Postgresql database. The database should be in bidirectional sync with Salesforce Account object. You can find the details about these in the trailhead link .
My heroku app page looks like this ,see the add ons in Resource tab.

II)Heroku connect home page , in mapping section we have to map with objects of salesforce org , once mapped it will fetch the data from Salesforce and that can be shown in Explorer tab.

Heroku postgres page:

So, till now we have created a database in Heroku which is in bidirectional sync with Salesforce Account object and an heroku connect add on. Till now no real time use 😁.
But what’s next ? How are you going to create a actual application ( not like clicking the ‘create app’ button in heroku 😁 ) which would be hosted in Heroku and use the data coming from salesforce.
III) Let’s dig into that..as you know heroku is a PaaS (platform as a service) ,hence you can build your application in any of the heroku supported language (Node.js ,Python, Java and many more ) and host it in heroku so that you dont need to think about other overheads like scaling , patching, maintaining the server etc for your app.
I created a small Account creation page written in node.js ( front end page in HTML and backend calculation in node.js) , the code editor I used is VScode and pushed it to github , you can find the code details Here. You can find a tutorial about how to install node.js in your machine and start coding in vscode here.
I am not going into details about how can we write a node.js app as that would be taken care by some other team ( I mean in your workplace ), purpose of this tutorial is to show how can a app be hosted in Heroku and can connect to Salesforce. But remember while connecting to the postgresql table you should use the connection string in your .JS file given by heroku (in the credentail page of postgres database). Use the URI as connection string in your .JS file.
And then push your code to github as repository .
You can download the code from my repository ( https://github.com/Debajyotipal/Account-update-code-for-heroku and use in your heroku app.
However, when you are actually using your code to heroku you should use config variable DATABASE_URL in your JS file , because the database connection string might be changed by heroku later in future but process.env.DATABASE_URL variable will always point towards the current connection string. A snippet is given here.


IV) So till now you have written your code and moved to Github , and in heroku you have connected to sfdc , still no connection between your code and sfdc data.
Now, the tricky part is to host or use your newly built app in heroku so that rest of the scaling, patching responsibility would be taken care by Heroku.
Go to your app deploy page (shown below) and choose the deployment process as github and connect to your github page.And make the automatic deploy on so that whenever you are pushing your code to github it will be reflected in your Heroku app.

And then click the open app button , it will open your first heroku hosted app page which I have shown above ( account creation page ). Any new acount created by this page would be stored in postgresql table and that will create a record in Account object.



Gist of steps:
I) Create a Heroku app , add 2 add ons — heroku connect and postgres database, connect to salesforce org using heroku connect.
II) Map your salesforce object in Heroku connect and fetch the data into postgres table.
III) Either download the node.js code into your VS code and do the necessary changes and push to your github or directly Deploy my github code in your heroku app using github deploy.
And your app is ready .
Few issues and points to remember :
- We have to install Heroku CLI to check the logs of your application , the command is ‘heroku logs -t’.
- I faced an issue that the data entered in postgres table were not getting moved to salesforce , reason of error can be seen in the table salesforce._trigger_log which is present in the postgres database provided by Heroku.For that we need to install postgres in local machine and open the database using pgadmin tool

- Learning different github commands in VScode terminal would be needed . Few are ,
git init — to initialize your git account.git remote add origin https://github.com/Debajyotipal/Account-update-code-for-heroku — To add a remote origin from terminal.git push origin +master — to push your code to github accountGit pull origin master — To pull your code from remote to local.git commit -a -m “Add existing file” –Committing your change ,its needed before you push your code.git commit — amend -m “an updated commit message” –Updating your current commit
benefits of using Heroku
- One might think instead of fetching the records from heroku postgres , we can directly fetch it from Salesforce using API but that might exceed the permitted limit , here as we are storing the records within heroku hence no API limit is enforced.
- As Heroku provides a platform to host the application hence no patching ,no scaling etc need to be maintained by developers . All will be taken care Heroku team.
Part II(Connecting Salesforce with RDS MySql using Mulesoft)
By this time you would be able to create a account record using a Heroku app written in node.js and stored in Postgres.
Now the second integration between Salesforce and AWS RDS Mysql table.You might have thought why did we enter the product name in the account page while we were entering the Account details , well , sometimes there can be scenario that your Product information is stored in some other legacy database but you need to show the product related information in your salesforce UI based on the Product name.
Here ,for example my Product database is stored in AWS RDS Mysql table. Point to remember this database instance should be behind a custom security group otherwise it can not be accessed to external users ( like mulesoft ).

So the idea is once we entered the product name as ‘Samsung’ and created a Salesforce record ,it will automatically fetch the Samsung related information from MqSql RDS table and update it in Salesforce in real time.
Flow is like this:

Steps to be followed to achieve that:
i) First create a salesforce topic , this topic would be fired whenever a Account is created or modified , and this will fetch the account id and product name details . ID is required to update the same record later and product name is required to get the product related information from RDS Database.

The complete query of the topic is
SELECT name,BillingAddress,BillingCity,BillingCountry,BillingState,Id,Product__c FROM Account WHERE CreatedDate > 2019–09–01T00:00:00.000Z OR LastModifiedDate > 2019–09–01T00:00:00.000Z
I selected few other information which are not required though, mainly product and ID is required.
ii) Second step is to put the product info data in the RDS Mqsql table using MySQL workbench , this needs to be installed in your machine and connected to RDS instance of AWS using the hostname endpoint provided by RDS .

iii)Third and last step is to create a Mulesoft flow to integrate between these 2 systems .I have created this flow in Anypoint platform , you can do it in Anypoint studio as well or have a 1 month subscription of Anypoint platform and write your flow.


In the first Salesforce connector, I have subscribed to the Salesforce topic I have created .
Then converted that fetched data into Json format using Transform dataweave and stored it into a Variable called accid.

Then create a database connection with AWS RDS Mysql table.

Here the host is provided by AWS (as shown in above screenshot ).
And write your query in the database connector.

Then transform the data into Salesforce readable format so that you can insert/update the data in sfdc.


Finally we need to call a SFDC connector to update the salesforce record with price and description of the product.
Thus you can see the price and description of the product is directly fetched from the AWS database in real time.
This is how we can integrate 2 different systems , utilizing mulesoft .
I am attaching a demo of this.
