Last active
July 11, 2018 00:36
-
-
Save benedwards44/86a25b78aee2beafb173a97322a50cec to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import beatbox | |
| import datetime | |
| SANDBOX = True | |
| USERNAME = '[email protected]' | |
| PASSWORD = 'google12' | |
| sf = beatbox._tPartnerNS | |
| svc = beatbox.Client() | |
| if SANDBOX: | |
| svc.serverUrl = svc.serverUrl.replace('login.', 'test.') | |
| def build_select_fields(object_name): | |
| """ | |
| Build the full list of select fields | |
| """ | |
| describe_result = svc.describeSObjects(object_name) | |
| select = '' | |
| for f in describe_result[sf.fields:]: | |
| if len(select) > 0: | |
| select += ',' | |
| select += str(f[sf.name]) | |
| return select | |
| def build_soql(object_name): | |
| """ | |
| Build the SOQL to retrieve all fields for the given object | |
| """ | |
| return "select " + build_select_fields(object_name) + " from " + object_name | |
| def get_records(object_name): | |
| """ | |
| Retrieve all records for the given object name | |
| """ | |
| # Login | |
| svc.login(USERNAME, PASSWORD) | |
| print 'Querying all records for %s' % object_name | |
| # Build the SOQL | |
| soql = build_soql(object_name) | |
| # The query result | |
| query_result = svc.query(soql) | |
| print 'Total #%s records to query' % query_result[sf.size] | |
| # The list of records to return | |
| records = [] | |
| # Keep querying until done is True | |
| while True: | |
| # Iterate over each row | |
| for row in query_result[sf.records:]: | |
| # Add the record to the list | |
| records.append(build_record_dict(row)) | |
| if str(query_result[sf.done]) == 'true': | |
| break | |
| # Query for more records | |
| query_result = svc.queryMore(str(query_result[sf.queryLocator])) | |
| return records | |
| def get_updated(object_name, num_days): | |
| """ | |
| Retrieve all records updated in the last n dayds | |
| """ | |
| # Login | |
| svc.login(USERNAME, PASSWORD) | |
| print 'Getting updated records for %s' % object_name | |
| updated_ids = [] | |
| for id in svc.getUpdated(object_name, datetime.datetime.today() - datetime.timedelta(num_days), datetime.datetime.today() + datetime.timedelta(1))[sf.ids:]: | |
| updated_ids.append(str(id)) | |
| print 'Total of %d updated records to retrieve' % len(updated_ids) | |
| print 'Retrieving full records...' | |
| records = [] | |
| for row in svc.retrieve(build_select_fields(object_name), object_name, updated_ids): | |
| # Add the record to the list | |
| records.append(build_record_dict(row)) | |
| return records | |
| def build_record_dict(row): | |
| """ | |
| Build the record dict from the pass row | |
| """ | |
| # The record to return | |
| record = {} | |
| for field in row[2:]: | |
| record[field._name[1]] = field._dir[0] if field._dir else None | |
| return record | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment