私の経験では、残りのレコードを取得するには sf.query(query=SOQL) を使用し、 sf.query_more(next_URL, True) を使用するのが最善であることがわかりました
クエリは 2000 レコードしか返さないため、さらにレコードを取得するには .query_more() を使用する必要があります
simple-salesforce ドキュメントから
SOQL クエリは次の方法で実行されます。
sf.query("SELECT Id, Email FROM Contact WHERE LastName = 'Jones'")
結果が特に大きいために、Salesforce が"nextRecordsUrl" : "/services/data/v26.0/query/01gD0000002HU6KIAW-2000"などのクエリ結果にnextRecordsUrlを追加する場合、次のいずれかを使用して追加の結果を取得できます。 ID または完全な URL (完全な URL を使用する場合は、2 番目の引数として「True」を渡す必要があります)
sf.query_more("01gD0000002HU6KIAW-2000")
sf.query_more("/services/data/v26.0/query/01gD0000002HU6KIAW-2000", True)
また、「=」の代わりに「in」を使用するようにSQLステートメントを変更して、一度に等しい複数の値を選択することもできます。そうすれば、さらにレコードを要求するまで、salesforce への API 呼び出しを 1 回だけ行うことができます。(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'])