Search code examples
pythonplotlyplotly-dashsimple-salesforce

Need Help Turning Salesforce Data Requested from Salesforce into Dataframe for Internal Dashboard


My problem lies with taking simple-salesforce queries out of the Salesforce and then Transforming them into a dataframe with Plotly Dashboard App.

I have tried to authenticate with Salesforce in Python (SUCCESS) I can request data and receive it with HTTP GET status (SUCCESS) I have an internal Dashboard App running Plotly Locally (SUCCESS) I have a general layout for the Dashboard App Locally (SUCCESS) Transforming Data query in Python into table/dataframe (FAILURE)

import os
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table
import salesforce_reporting
from simple_salesforce import Salesforce
import requests
import pandas as pd
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, State
import plotly.graph_objects as go



# Salesforce User Session and Fields
# ==================================
from simple_salesforce import SalesforceAPI
sf = SalesforceAPI('<your username>', '<your salesforce password>', '<your salesforce token>')

Fields = ['isMosAlert__c',
              'Milestone_violated__c',
              'First_Reply__c',
              'CaseNumber',
              'Environment2__r.Name',
              'Owner.Name',
              'Severity_Level__c',
              'Status',
              'Subject',
              'URL_for_support__c'
]

Items = str(Fields[1:10]).strip('[]')
print(Items).replace("'", "")
sf.query("SELECT %s from Case"% ','.join(Fields)  )

#fig = go.Figure(data=[go.Table(header=dict(values=['A Scores', 'B Scores']),
#                 cells=dict(values=[[100, 90, 80, 90], [95, 85, 75, 95]]))
#                     ])

fig = go.Figure(data=[go.Table(header=dict(values=Fields),
        cells=dict(values=[sf.query("SELECT %s from Case"% ','.join(Fields))]))
        ])

fig.show()

I actually do get a table, but all it shows is headers which I have defined as the fields data. Should the salesforce query be set to tablea variable? I want to get my table with Salesforce Data looking like the following picture that I took from Plotly Documentation enter image description here

Actual information I get with the salesforce query returns like this while in python interactively.

(u'isMosAlert__c', True), (u'Milestone_violated__c', False), (u'First_Reply__c', True), (u'CaseNumber', u'1850637'), (u'Environment2__r', OrderedDict([(u'attributes', OrderedDict([(u'type', u'Environment__c'), (u'url', u'/services/data/v27.0/sobjects/Environment__c/a2X440000024ZkzEAE')])), (u'Name', u'MCP500 Production')])), (u'Owner', OrderedDict([(u'attributes', OrderedDict([(u'type', u'Name'), (u'url', u'/services/data/v27.0/sobjects/Group/00GE0000003YOIEMA4')])), (u'Name', u'L1 Support Group')])), (u'Severity_Level__c', None), (u'Status', u'Ignored'), (u'Subject', u'elasticsearch - critical'), (u'URL_for_Support__c', u'https://mirantis.my.salesforce.com/5004400000ykxS9')])])])

Solution

  • So I found out how to do this with simple-salesforce.

    from pandas import DataFrame
    from simple_salesforce import SalesforceAPI
    sf = SalesforceAPI('<insert_username>', '<insert_password>', '<insert_token>')
    
    # Saleforce Secure Connection TBD
    # ===============================
    # sf_sec = Salesforce(username = username,
    #               password = password,
    #               security_token= token,
    #               instance_url = instance,
    #               sandbox = isSandbox)
    
    # Function to Return Salesforce SOQL Queries as Dataframes
    # =========================================================
    def sql_query(query_str):
        qry_result = sf.query(query_str)
        print('Record Count {0}'.format(qry_result['totalSize']))
        is_done = qry_result['done']
        if is_done:
            df = DataFrame(qry_result['records'])
        while not is_done:
            try:
                if not qry_result['done']:
                    df = df.append(DataFrame(qry_result['records']))
                    qry_result = sf.query_more(qry_result['nextRecordsUrl'], True)
                else:
                    df = df.append(DataFrame(qry_result['records']))
                    is_done = True
                    print('completed')
                    break
            except NameError:
                df = DataFrame(qry_result['records'])
                sf.query_more(qry_result['nextRecordsUrl'], True)
        df = df.drop('attributes', axis=1)
        return df
    soql_test = 'SELECT Milestone_violated__c, First_Reply__c, CaseNumber, Environment2__r.Name,' \
                ' Owner.Name, Severity_Level__c, Status, Subject, URL_for_support__c from Case'
    res = sql_query(soql_test)
    print(res)
    
    # SAMPLE SOQL COMMANDS
    # ====================
    # SOQL('SELECT Id FROM CASE')
    

    The data takes awhile to be processed, but the returned Data looks like the following depending on what object API names your salesforce organization has:

    Record Count 307027
    completed
          Milestone_violated__c                        ...                                                         URL_for_Support__c
    0                     False                        ...                          https://mirantis.my.salesforce.com/500E000000Z...
    1                     False                        ...                          https://mirantis.my.salesforce.com/500E000000Z...
    2                     False                        ...                          https://mirantis.my.salesforce.com/500E000000Z...