Create your own Salesforce Dashboard in Python with Dash

In this post we will be exploring how you can retrieve your company data using the Salesforce API and make some exciting apps with Dash.

To interact with the Salesforce data in python, we will use the Simple-Salesforce module which is a basic Salesforce.com REST API client that makes using the API very easy.

Note that the default Salesforce Developer account contains sample data which could be used to test the code that we are going to write. You can create here an account.

Salesforce Authentification

To gain access to Salesforce you have several methods that you can find in the documentation.
In this tutorial we will be using the security token method. You can find your security token in your account parameters, or in the confirmation email and log in using the Salesforce object.

All you have to do is create a Salesforce object passing your credentials and your token. Your salesforce object will be used to retrieve or to push data.

Queries

To retrieve your records you just have to call the query method and pass as a parameter your Salesforce Object Query Language string.
You can learn more about the SOQL syntaxe in the documentation.

Let’s try to retrieve for example all the leads records.

1

query_result=sf.query("SELECT Id, LeadSource, Status FROM Lead")

The query_result variable now contains an ordereddict with three keys:
– ‘totalSize’: point to an integer corresponding to the total size of the records.
– ‘done’: point to a boolean indicating query success.
– ‘records’: point to a list of ordereddicts that contains the records.

Now it’s time to make some nice visualizations using your data!

Visualize your data with dash

Let’s first convert the ordereddict to a pandas dataframe so we can enjoy its power.