Guest User

Untitled

a guest
Jun 9th, 2018
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. from simple_salesforce import Salesforce
  2. import pytz
  3. import datetime
  4.  
  5. CLIENT_ID = '3MVG959Nd8JMmavRjCqjEGuLnGzR.CNuRMjOLwZKB_5gIxL0CvEXzU6Rqpf6pjdHu3cJXVvyYVrCwrxyLRy_i'
  6. SANDBOX = True
  7. USERNAME = 'ben.edwards@acs.org.au.staging'
  8. PASSWORD = 'google12'
  9.  
  10. sf = Salesforce(
  11. client_id=CLIENT_ID,
  12. username=USERNAME,
  13. password=PASSWORD,
  14. security_token='',
  15. sandbox=SANDBOX
  16. )
  17.  
  18. objects = {}
  19. for obj in sf.describe()["sobjects"]:
  20. objects[obj.get('name')] = obj.get('fields')
  21.  
  22.  
  23. def get_records(object_name):
  24. """
  25. Retrieve all fields and records for a given object name
  26. """
  27.  
  28. print 'Querying for all records for %s' % object_name
  29. total_size = sf.query('select count() from ' + object_name).get('totalSize')
  30. print 'There will be %d total records to query' % total_size
  31.  
  32. # Execute the query
  33. query_result = sf.query(get_query(object_name))
  34.  
  35. # Add the records to the array
  36. records = query_result.get('records')
  37.  
  38. while 'nextRecordsUrl' in query_result:
  39.  
  40. print 'Current records size: ' + str(len(records))
  41. print 'Querying for more records...'
  42. query_result = sf.query_more(query_result.get('nextRecordsUrl'), True)
  43. records.extend(query_result.get('records'))
  44.  
  45. print 'Finishing querying all %s records. Total record size is %d' % (object_name, len(records))
  46.  
  47. return records
  48.  
  49.  
  50. def get_updated(object_name, num_days):
  51. """
  52. Retrieve records that were updated in the last x days
  53. """
  54.  
  55. end = datetime.datetime.now(pytz.UTC)
  56. return getattr(sf, object_name).updated(end - datetime.timedelta(days=num_days), end)
  57.  
  58.  
  59. def get_query(object_name):
  60. """
  61. Build the query to retrieve all fields for the object
  62. """
  63.  
  64. # Describe the object to retrieve the metadata
  65. object_describe = getattr(sf, object_name).describe()
  66.  
  67. # Build the field names
  68. field_names = [field['name'] for field in object_describe.get('fields')]
  69.  
  70. # Return the query
  71. return "SELECT {} FROM {}".format(','.join(field_names), object_name)
  72.  
  73.  
  74. """
  75. accounts = get_records('Account')
  76. contacts = get_records('Contact')
  77. memberships = get_records('Contract')
  78. users = get_records('User')
  79. """
Add Comment
Please, Sign In to add comment