Last active
June 23, 2016 13:52
-
-
Save mtbdeano/2266f328126b2380f734c785c3e29bbc to your computer and use it in GitHub Desktop.
simple_salesforce Account ActivityHistory extractor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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