salesforce api 在 Python 的 for 循环中调用 returns 太多响应

salesforce api call returns too many responses within for loop in Python

我正在进行销售人员批量 API 调用以获取数据。我正在使用 simple_salesforce 库。我想获取我的 ID 等于特定值的数据,我还需要 return 一些响应,因为我有一个 ID 列表。我的数据如下所示:

ids_dict = [{'ID-1010': 'abc'}, {'ID-1020': 'def'}]

代码如下:

for key, value in ids_dict.items():
    desired_opp = value
    sql = sf.bulk.OpportunityLineItem.query("SELECT Name, Price FROM OpportunityLineItem where Opportunity_ID__c = '%s'" % desired_opp)
    sql_response = []
    sql_response.append(sql)

正在 returned 的是一个列表,其中包含具有 def 个 ID 的多个响应。我只需要两个回复即可获得尊重的 ID。

根据我的经验,我发现最好使用 sf.query(query=SOQL) 并使用 sf.query_more(next_URL, True) 来获取其余记录

由于只查询了returns 2000条记录,需要使用.query_more()才能获取更多记录

来自simple-salesforce docs

SOQL queries are done via:

sf.query("SELECT Id, Email FROM Contact WHERE LastName = 'Jones'")

If, due to an especially large result, Salesforce adds a nextRecordsUrl to your query result, such as "nextRecordsUrl" : "/services/data/v26.0/query/01gD0000002HU6KIAW-2000", you can pull the additional results with either the ID or the full URL (if using the full URL, you must pass ‘True’ as your second argument)

sf.query_more("01gD0000002HU6KIAW-2000")
sf.query_more("/services/data/v26.0/query/01gD0000002HU6KIAW-2000", True)

也许您还想将 sql 语句更改为使用 "in" 而不是 "=" 到 select 多个值,它可以一次等于多个值。这样一来,您只需 API 调用 salesforce,直到您请求更多记录。 (删除多次少于 2000 条记录搜索时的浪费呼叫)

这是一个使用这个的例子

 data = [] # list to hold all the records

 SOQL = "SELECT Name, Price FROM OpportunityLineItem where Opportunity_ID__c in ('abc', 'def')"

 results = sf.query(query=SOQL) # api call

 ## loop through the results and add the records
 for rec in results['records']:
     rec.pop('attributes', None) # remove extra data
     data.append(rec) # add the record to the list


 ## check the 'done' attrubite in the response to see if there are more records
 ## While 'done' == False (more records to fetch) get the next page of records
 while(results['done'] == False):
     ## attribute 'nextRecordsUrl' holds the url to the next page of records
     results = sf.query_more(results['nextRecordsUrl', True])

     ## repeat the loop of adding the records
     for rec in results['records']:
         rec.pop('attributes', None)
         data.append(rec)

遍历记录并使用数据

 ## loop through the records and get their attribute values
 for rec in data:
     # the attribute name will always be the same as the salesforce api name for that value
     print(rec['Name'])
     print(rec['Price'])