Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from simple_salesforce import Salesforce
- import pytz
- import datetime
- CLIENT_ID = '3MVG959Nd8JMmavRjCqjEGuLnGzR.CNuRMjOLwZKB_5gIxL0CvEXzU6Rqpf6pjdHu3cJXVvyYVrCwrxyLRy_i'
- SANDBOX = True
- USERNAME = 'ben.edwards@acs.org.au.staging'
- PASSWORD = 'google12'
- sf = Salesforce(
- client_id=CLIENT_ID,
- username=USERNAME,
- password=PASSWORD,
- security_token='',
- sandbox=SANDBOX
- )
- objects = {}
- for obj in sf.describe()["sobjects"]:
- objects[obj.get('name')] = obj.get('fields')
- def get_records(object_name):
- """
- Retrieve all fields and records for a given object name
- """
- print 'Querying for all records for %s' % object_name
- total_size = sf.query('select count() from ' + object_name).get('totalSize')
- print 'There will be %d total records to query' % total_size
- # Execute the query
- query_result = sf.query(get_query(object_name))
- # Add the records to the array
- records = query_result.get('records')
- while 'nextRecordsUrl' in query_result:
- print 'Current records size: ' + str(len(records))
- print 'Querying for more records...'
- query_result = sf.query_more(query_result.get('nextRecordsUrl'), True)
- records.extend(query_result.get('records'))
- print 'Finishing querying all %s records. Total record size is %d' % (object_name, len(records))
- return records
- def get_updated(object_name, num_days):
- """
- Retrieve records that were updated in the last x days
- """
- end = datetime.datetime.now(pytz.UTC)
- return getattr(sf, object_name).updated(end - datetime.timedelta(days=num_days), end)
- def get_query(object_name):
- """
- Build the query to retrieve all fields for the object
- """
- # Describe the object to retrieve the metadata
- object_describe = getattr(sf, object_name).describe()
- # Build the field names
- field_names = [field['name'] for field in object_describe.get('fields')]
- # Return the query
- return "SELECT {} FROM {}".format(','.join(field_names), object_name)
- """
- accounts = get_records('Account')
- contacts = get_records('Contact')
- memberships = get_records('Contract')
- users = get_records('User')
- """
Add Comment
Please, Sign In to add comment