使用python导入salesforce报告数据

时间:2014-04-04 03:56:31

标签: python-2.7 salesforce

我是sfdc的新手。我的报告已由用户创建。我想使用python将报告的数据转储到csv / excel文件中。 我看到有几个python包。但是我的代码出错了

from simple_salesforce import Salesforce
sf = Salesforce(instance_url='https://cs1.salesforce.com', session_id='')
sf = Salesforce(password='xxxxxx', username='xxxxx', organizationId='xxxxx')

我是否可以获得设置API的基本步骤和一些示例代码

4 个答案:

答案 0 :(得分:5)

这对我有用:

import requests
import csv
from simple_salesforce import Salesforce
import pandas as pd


sf = Salesforce(username=your_username, password=your_password, security_token = your_token)


login_data = {'username': your_username, 'password': your_password_plus_your_token}


with requests.session() as s:
    d = s.get("https://your_instance.salesforce.com/{}?export=1&enc=UTF-8&xf=csv".format(reportid), headers=sf.headers, cookies={'sid': sf.session_id})

d.content将包含一串逗号分隔值,您可以使用csv模块读取这些值。

我从那里将数据带入pandas,因此函数名称和import pandas。我删除了将数据放入DataFrame的函数的其余部分,但是如果您对该如何完成感兴趣请告诉我。

答案 1 :(得分:1)

如果有帮助,我想根据Obol的意见写出我现在用来回答这个问题的步骤(2018年8月)。作为参考,我遵循了https://github.com/cghall/force-retrieve/blob/master/README.md上关于salesforce_reporting软件包的自述文件说明。

要连接到Salesforce:

from salesforce_reporting import Connection, ReportParser

sf = Connection(username='your_username',password='your_password',security_token='your_token')

然后,将我想要的报告放入Pandas DataFrame中:

report = sf.get_report(your_reports_id)
parser = salesforce_reporting.ReportParser(report)
report = parser.records_dict()
report = pd.DataFrame(report)

如果您愿意的话,还可以将上面的四行简化为一个,就像这样:

report = pd.DataFrame(salesforce_reporting.ReportParser(sf.get_report(your_reports_id)).records_dict())

我从README中遇到的一个区别是sf.get_report('report_id', includeDetails=True)抛出了一个错误,指出get_report() got an unexpected keyword argument 'includeDetails'。只需将其删除似乎可以使代码正常工作。

report现在可以通过report.to_csv('report.csv',index=False)导出,或直接进行操作。

编辑:parser.records()更改为parser.records_dict(),因为这允许DataFrame具有已列出的列,而不是对其进行数字索引。

答案 2 :(得分:0)

下面的代码很长,可能仅用于我们的用例,但基本思想如下:

找出日期间隔长度和其他所需的过滤条件,以免遇到“超过2'000”的限制。就我而言,我可以使用每周日期范围过滤器,但需要应用一些其他过滤器

然后像这样运行它:

report_id = '00O4…'
sf = SalesforceReport(user, pass, token, report_id)
it = sf.iterate_over_dates_and_filters(datetime.date(2020,2,1),
     'Invoice__c.InvoiceDate__c', 'Opportunity.CustomField__c', 
     [('a', 'startswith'), ('b', 'startswith'), …])
for row in it:
  # do something with the dict

自2020年2月1日以来,迭代器每周都要经历一次(如果您需要每日迭代器或每月一次,则需要更改代码,但更改应该很小),并应用过滤器CustomField__c.startswith('a '),然后是CustomField__c.startswith('b'),...并充当生成器,因此您无需自己弄乱过滤器。

如果有一个查询返回的行超过2000行,则迭代器将引发Exception,只是为了确保数据不完整。

此处警告:SF每小时最多可查询500个查询。假设您有一年的时间(52周)和10个其他过滤器,那么您已经遇到了该限制。

这是课程(取决于simple_salesforce

import simple_salesforce
import json
import datetime

"""
helper class to iterate over salesforce report data
and manouvering around the 2000 max limit
"""

class SalesforceReport(simple_salesforce.Salesforce):
  def __init__(self, username, password, security_token, report_id):
    super(SalesforceReport, self).__init__(username=username, password=password, security_token=security_token)
    self.report_id = report_id
    self._fetch_describe()

  def _fetch_describe(self):
    url = f'{self.base_url}analytics/reports/{self.report_id}/describe'
    result = self._call_salesforce('GET', url)
    self.filters = dict(result.json()['reportMetadata'])

  def apply_report_filter(self, column, operator, value, replace=True):
    """
    adds/replaces filter, example:
    apply_report_filter('Opportunity.InsertionId__c', 'startsWith', 'hbob').
    For date filters use apply_standard_date_filter.

    column:   needs to correspond to a column in your report, AND the report
              needs to have this filter configured (so in the UI the filter
              can be applied)
    operator: equals, notEqual, lessThan, greaterThan, lessOrEqual,
              greaterOrEqual, contains, notContain, startsWith, includes
              see https://sforce.co/2Tb5SrS for up to date list
    value:    value as a string
    replace:  if set to True, then if there's already a restriction on column
              this restriction will be replaced, otherwise it's added additionally
    """
    filters = self.filters['reportFilters']
    if replace:
      filters = [f for f in filters if not f['column'] == column]
    filters.append(dict(
      column=column, 
      isRunPageEditable=True, 
      operator=operator, 
      value=value))
    self.filters['reportFilters'] = filters

  def apply_standard_date_filter(self, column, startDate, endDate):
    """
    replace date filter. The date filter needs to be available as a filter in the
    UI already

    Example: apply_standard_date_filter('Invoice__c.InvoiceDate__c', d_from, d_to)

    column: needs to correspond to a column in your report
    startDate, endDate: instance of datetime.date
    """
    self.filters['standardDateFilter'] = dict(
      column=column,
      durationValue='CUSTOM',
      startDate=startDate.strftime('%Y-%m-%d'),
      endDate=endDate.strftime('%Y-%m-%d')
    )

  def query_report(self):
    """
    return generator which yields one report row as dict at a time
    """
    url = self.base_url + f"analytics/reports/query"
    result = self._call_salesforce('POST', url, data=json.dumps(dict(reportMetadata=self.filters)))
    r = result.json()
    columns = r['reportMetadata']['detailColumns']
    if not r['allData']:
      raise Exception('got more than 2000 rows! Quitting as data would be incomplete')
    for row in r['factMap']['T!T']['rows']:
      values = []
      for c in row['dataCells']:
        t = type(c['value'])
        if t == str or t == type(None) or t == int:
          values.append(c['value'])
        elif t == dict and 'amount' in c['value']:
          values.append(c['value']['amount'])
        else:
          print(f"don't know how to handle {c}")
          values.append(c['value'])
      yield dict(zip(columns, values))

  def iterate_over_dates_and_filters(self, startDate, date_column, filter_column, filter_tuples):
    """
    return generator which iterates over every week and applies the filters 
    each for column
    """
    date_runner = startDate
    while True:
      print(date_runner)
      self.apply_standard_date_filter(date_column, date_runner, date_runner + datetime.timedelta(days=6))
      for val, op in filter_tuples:
        print(val)
        self.apply_report_filter(filter_column, op, val)
        for row in self.query_report():
          yield row
      date_runner += datetime.timedelta(days=7)
      if date_runner > datetime.date.today():
        break

答案 3 :(得分:0)

对于任何试图将报告下载到 DataFrame 中的人来说,这就是您的做法(我添加了一些注释和链接以供澄清):

import pandas as pd
import csv
import requests
from io import StringIO
from simple_salesforce import Salesforce

# Input Salesforce credentials:
sf = Salesforce(
    username='johndoe@mail.com', 
    password='<password>', 
    security_token='<security_token>') # See below for help with finding token 

# Basic report URL structure:
orgParams = 'https://<INSERT_YOUR_COMPANY_NAME_HERE>.my.salesforce.com/' # you can see this in your Salesforce URL
exportParams = '?isdtp=p1&export=1&enc=UTF-8&xf=csv'

# Downloading the report:
reportId = 'reportId' # You find this in the URL of the report in question between "Report/" and "/view"
reportUrl = orgParams + reportId + exportParams
reportReq = requests.get(reportUrl, headers=sf.headers, cookies={'sid': sf.session_id})
reportData = reportReq.content.decode('utf-8')
reportDf = pd.read_csv(StringIO(reportData))

您可以按照 this page

底部的说明获取您的令牌