需要帮助将 Salesforce 请求的 Salesforce 数据转换为内部仪表板的 Dataframe

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

我的问题在于从 Salesforce 中提取 simple-salesforce 查询,然后使用 Plotly Dashboard App 将它们转换为数据框。

我已尝试在 Python 中使用 Salesforce 进行身份验证(成功) 我可以请求数据并使用 HTTP GET 状态接收数据(成功) 我有一个内部仪表板应用程序 运行 Plotly Locally(成功) 我在本地有仪表板应用程序的总体布局(成功) 将 Python 中的数据查询转换为 table/dataframe(失败)

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()

我确实得到了 table,但它只显示 headers,我将其定义为字段数据。 salesforce 查询是否应设置为 a variable? I want to get my table with Salesforce Data looking like the following picture that I took from Plotly Documentation

我通过 salesforce 查询 returns 在 python 中以交互方式获得的实际信息。

(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')])])])

所以我找到了如何使用 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')

数据需要一段时间才能处理,但返回的数据如下所示,具体取决于您的 salesforce 组织具有的对象 API 名称:

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...