Skip to content

Instantly share code, notes, and snippets.

@mtbdeano
Last active June 23, 2016 13:52
Show Gist options
  • Save mtbdeano/2266f328126b2380f734c785c3e29bbc to your computer and use it in GitHub Desktop.
Save mtbdeano/2266f328126b2380f734c785c3e29bbc to your computer and use it in GitHub Desktop.
simple_salesforce Account ActivityHistory extractor
ACTIVITY_QUERY = """
SELECT Id,
(SELECT Id, ActivityDate, WhatId, WhoId, Description
FROM ActivityHistories)
FROM Account
"""
ACTIVITY_FIELDS = ["Id", "AccountId", "ActivityDate", "WhatId", "WhoId", "Description"]
result = sf.query(ACTIVITY_QUERY)
with open(sys.argv[1], "w") as outf:
output = UnicodeWriter(outf)
output.writerow(ACTIVITY_FIELDS)
# do/while ... LOLz
while True:
# process the results batch
num_act = 0
for row in result['records']:
if row['ActivityHistories']:
num_act += len(row['ActivityHistories']['records'])
for activity in row['ActivityHistories']['records']:
if activity['Activity_Type__c'] is not None:
output.writerow([activity['Id'],
row['Id'],
activity['ActivityDate'],
activity['WhatId'],
activity['WhoId'],
activity['Description'][:256]])
#print("processed {} accounts {} activities".format(len(result['records']), num_act))
# exit if no more results
nru = result.get('nextRecordsUrl')
if nru is None:
break
result = sf.query_more(nru, True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment