gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:29:04 24 4
gpt4 key购买 nike

我的问题在于从 Salesforce 中取出简单的销售人员查询,然后使用 Plotly Dashboard 应用程序将它们转换为数据框。

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

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

我实际上确实得到了一个表格,但它显示的只是我定义为字段数据的标题。是否应将 salesforce 查询设置为 table一个变量?我想让我的带有 Salesforce 数据的表格看起来像我从 Plotly 文档 enter image description here 中获取的下图所示

在 python 中,我通过 salesforce 查询返回的实际信息以交互方式返回。

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

处理数据需要一段时间,但返回的数据如下所示,具体取决于您的销售人员组织拥有的对象 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...

关于python - 需要帮助将从 Salesforce 请求的 Salesforce 数据转换为内部仪表板的 Dataframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57634491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com