How Can We Help?
< All Topics
Print

Exporting all the records of an object with all fields

For example, exporting all opportunities with all fields. You will get a list of dictionaries. You can then easily export this list to a CSV file or to a Pandas dataframe.

object_to_export = "Opportunity"
limit = 10000

describe = sf_api_call('/services/data/v40.0/sobjects/%s/describe' % object_to_export)

if not describe.get('queryable', False):
raise Exception("This object is not queryable")

fields = [f['name'] for f in describe.get('fields')]

query = "SELECT %s FROM %s LIMIT %i" % (
", ".join(fields),
object_to_export,
limit
)

call = sf_api_call('/services/data/v40.0/queryAll/', {'q': query})

rows = call.get('records', [])
# for obj in call.get('records', []):
# print(obj)

next = call.get('nextRecordsUrl', None)

while next:
call = sf_api_call(next)
rows.extend(call.get('records', []))
# for obj in call.get('records', []):
# print(obj)
next = call.get('nextRecordsUrl', None)

# print(rows)

Please suggest edits or add your comments.

Your email address will not be published. Required fields are marked *

Scroll to Top