SalesforceAPI调用在Python中的for循环中返回太多响应

SalesforceAPI调用在Python中的for循环中返回太多响应,python,sql,for-loop,salesforce,simple-salesforce,Python,Sql,For Loop,Salesforce,Simple Salesforce,我正在进行salesforce批量API调用以获取数据。我正在使用一个简单的_salesforce库。我想在id等于特定值的地方获取数据,我还需要返回一些响应,因为我有一个id列表。我的数据如下所示: ids_dict = [{'ID-1010': 'abc'}, {'ID-1020': 'def'}] 以下是代码: for key, value in ids_dict.items(): desired_opp = value sql = sf.bulk.Opportunity

我正在进行salesforce批量API调用以获取数据。我正在使用一个简单的_salesforce库。我想在id等于特定值的地方获取数据,我还需要返回一些响应,因为我有一个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)

返回的是一个带有
def
id的多个响应的列表。在这里,我只需要两个对尊重ID的响应。

根据我的经验,我发现最好使用sf.query(query=SOQL)和sf.query\u more(next\u URL,True)来获取其余记录

因为查询只返回2000条记录,所以需要使用.query\u more()来获取更多记录

SOQL查询通过以下方式完成:

如果由于一个特别大的结果,Salesforce在您的查询结果中添加了一个nextRecordsUrl,例如“nextRecordsUrl”:“/services/data/v26.0/query/01gD0000002HU6KIAW-2000”,您可以使用ID或完整URL(如果使用完整URL,则必须将“True”作为第二个参数传递)

另外,您可能希望将sql语句更改为使用“in”而不是“=”来选择它可以同时等于的多个值。这样,您只需对salesforce进行一次API调用,直到您请求更多记录为止。(删除多个少于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'])

sql\u响应
置于循环之外。注意:.@Parfait我试过了-仍然有太多的响应我们看不到你的数据,所以一定是你的查询。Salesforce内部如何运行相同的查询?
 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'])