-
Notifications
You must be signed in to change notification settings - Fork 0
/
S2S-01-owner-names-sync.py
105 lines (76 loc) · 3.73 KB
/
S2S-01-owner-names-sync.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/python
"""
This script is intended to update several custom fields
on the Account, Lead and Contact objects in SFDC. The
name of each respective owner is pulled and the custom
free-text field is updated.
"""
import sys
import datetime
import config
import logging
from env import MySqlTask
from env import SalesforceTask
# SFDC Fields
accountOwnerField = 'Account_Owner_Name__c'
leadOwnerField = 'Lead_Owner_Name__c'
contactOwnerField = 'Contact_Owner_Name__c'
accountSubscriptionField = 'Subscription_Plan__c'
sf_batchSize = 200
# Start logging
if config.logLevel == "info":
logHandler = logging.basicConfig(format='%(asctime)s | %(levelname)s | %(filename)s | %(message)s', level=logging.INFO, filename=config.logFile)
else:
logHandler = logging.basicConfig(format='%(asctime)s | %(levelname)s | %(filename)s | %(message)s', level=logging.DEBUG, filename=config.logFile)
def processQueryResults(fieldName, data):
users = {}
for user in data:
if user['Owner']['Name'] != user[fieldName]:
users[user['Id']] = user['Owner']['Name']
return users
if __name__ == "__main__":
try:
# Create object for internal database methods (mySQL)
mysqlDb = MySqlTask(config.mysql_username, config.mysql_password, config.mysql_host, config.mysql_database)
# Create object for Salesforce methods
sfdc = SalesforceTask(config.sfUser, config.sfPass, config.sfApiToken)
logging.info("Logging into SFDC...")
######################## SFDC Accounts ########################
# Get the last modified timestamp
sfdcLastModified = sfdc.sfdc_timestamp()
# Grab the last time Accounts were successfully updated
scriptLastRun = mysqlDb.pull_job_timestamp("SFDC_ACCOUNT_OWNERS_LM")
query = "SELECT Id, Name, OwnerId, Owner.Name, Account_Owner_Name__c FROM Account WHERE LastModifiedDate > {0}".format(scriptLastRun)
results = sfdc.sfdc_query(query)
accountUpdates = processQueryResults(accountOwnerField, results['results'])
if len(accountUpdates):
print "Processing {0} Accounts".format(len(accountUpdates))
sfdc.update_sfdc_object('Account', accountOwnerField, accountUpdates, sf_batchSize)
mysqlDb.update_job_timestamp("SFDC_ACCOUNT_OWNERS_LM", sfdcLastModified)
######################## SFDC Leads ########################
# Get the last modified timestamp
sfdcLastModified = sfdc.sfdc_timestamp()
# Grab the last time Leads were successfully updated
scriptLastRun = mysqlDb.pull_job_timestamp("SFDC_LEAD_OWNERS_LM")
query = "SELECT Id, Name, OwnerId, Owner.Name, Lead_Owner_Name__c FROM Lead WHERE LastModifiedDate > {0}".format(scriptLastRun)
results = sfdc.sfdc_query(query)
leadUpdates = processQueryResults(leadOwnerField, results['results'])
if len(leadUpdates):
print "Processing {0} Leads".format(len(leadUpdates))
sfdc.update_sfdc_object('Lead', leadOwnerField, leadUpdates, sf_batchSize)
mysqlDb.update_job_timestamp("SFDC_LEAD_OWNERS_LM", sfdcLastModified)
######################## SFDC Contacts ########################
# Get the last modified timestamp
sfdcLastModified = sfdc.sfdc_timestamp()
# Grab the last time Contacts were successfully updated
scriptLastRun = mysqlDb.pull_job_timestamp("SFDC_CONTACT_OWNERS_LM")
query = "SELECT Id, Name, OwnerId, Owner.Name, Contact_Owner_Name__c FROM Contact WHERE LastModifiedDate > {0}".format(scriptLastRun)
results = sfdc.sfdc_query(query)
contactUpdates = processQueryResults(contactOwnerField, results['results'])
if len(contactUpdates):
print "Processing {0} Contacts".format(len(contactUpdates))
sfdc.update_sfdc_object('Contact', contactOwnerField, contactUpdates, sf_batchSize)
mysqlDb.update_job_timestamp("SFDC_CONTACT_OWNERS_LM", sfdcLastModified)
except Exception, err:
logging.exception("Exception in: " + str(sys.argv[0]))
sys.exit()