我正在尝试从csv文件中读取数据,并使用Python将/ upsert(取决于UniqueId的存在)插入到Salesforce中。
我知道我必须使用迭代器从cvs文件中读取数据,将它们放入List中,但我不知道如何从该List中获取数据并在Salesforce中放入(插入或插入)数据。
每个csv文件包含两个字段(UniqueId,Selection)。
我是否使用enumerate
?
这是我到目前为止(行动发生的地方):
如何将数据提取到Salesforce?
InFiles = glob.glob(inputFolderName + '/SurveySelection*.csv')
InFileslist = [] ##List to put file name
for InFile in InFiles:
thenStamp = os.path.getmtime(InFile)
if (thenStamp >= unix_time(dt1)) and (thenStamp >= unix_time(dt2)):
InFileslist.append(InFile) ## Expression to append files in certain time range
for filename in InFileslist:
file = open(filename, 'r')
lines = file.readlines()
答案 0 :(得分:1)
不太确定您的要求是什么,但如果您没有集成两个系统,为什么不直接使用salesforce提供的工具(如数据导入向导或Salesforce DataLoader)直接导入数据?如果您正在集成系统,那么您应该考虑使用Salesforce REST API,或使用Salesforce python sdk
pip install salesforce-python-sdk
答案 1 :(得分:0)
你现在和我大约8个月前一样。我已成功将MySQL数据库集成到Salesforce中。基本上客户会在我们的网站上注册产品,然后我让python从数据库中提取数据,将其全部格式化为xlsx文件,然后使用简单的salesforce包将该数据发送到salesforce,这非常有效。以下是一个代码片段,可以帮助您入门。如果您有任何问题,请告诉我。
import os
import sys
import xlrd
import xlwt
import time
import base64
import smtplib
import MySQLdb
import logging
import datetime
import collections
from xlrd.sheet import ctype_text
from collections import OrderedDict
from simple_salesforce import SFType
from simple_salesforce import Salesforce
from xlsxwriter.workbook import Workbook
from MySQLdb import OperationalError
from mysql.connector import MySQLConnection, Error
logger = logging.getLogger('view_rows')
hdlr = logging.FileHandler('C:\Users\Chris\Documents\Email Logs\myapp.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.WARNING)
# MySQL Database Credentials
ur = 'user_name' # your username
pd = 'my_password' # your password
ht = 'localhost' # your host
pt = 3306
db = 'my_database' # database where your table is stored
con = MySQLdb.connect(user=ur, passwd=pd, host=ht, port=pt, db=db)
cursor = con.cursor()
con.autocommit(True)
# Salesforce Credentials
sf = Salesforce(username='my_Salesforce_Email@mail.com', password='my_SF_password', security_token='my_Generated_token_from_Salesforce', client_id='This does not really matter')
orderedDict = collections.OrderedDict()
# Gets the new customers that have created an account on the Warranty Portal
def GetNewContact():
# Creates a xlsx file
workbook = Workbook('outfile.xlsx')
sheet = workbook.add_worksheet("Warranty Info")
query = "SELECT RC.cus_id, RC.cus_username, RC.cus_subsc FROM my_database.rma_customer RC INNER JOIN my_database.rma_customer_address CA ON RC.cus_id=CA.cus_id WHERE CA.cus_address_type=0 AND CA.cus_updated=1"
query0 = "SELECT RCD.cus_firstname, RCD.cus_lastname, RCD.cus_cell_phone FROM my_database.rma_customer_details RCD INNER JOIN my_database.rma_customer_address CA ON RCD.cus_id=CA.cus_id WHERE CA.cus_address_type=0 AND CA.cus_updated=1"
query1 = "SELECT CA.cus_address_1, CA.cus_address_2, CA.cus_city, CA.cus_postcode FROM my_database.rma_customer_address CA LEFT JOIN my_database.rma_customer RC ON CA.cus_id=RC.cus_id WHERE CA.cus_address_type=0 AND CA.cus_updated=1"
cursor.execute(query)
headers = ['ID', 'Email', 'Newsletter', 'FirstName', 'LastName', 'Phone', 'Street', 'APT', 'City', 'Postal']
for i, header in enumerate(headers):
sheet.write(0, i, header)
for r, row in enumerate(cursor.fetchall()):
r = r + 1
for c, col in enumerate(row):
sheet.write(r, c, col)
cursor.execute(query0)
for r, row in enumerate(cursor.fetchall()):
r = r + 1
for c, col in enumerate(row):
sheet.write(r, c+3, col)
cursor.execute(query1)
for r, row in enumerate(cursor.fetchall()):
r = r + 1
for c, col in enumerate(row):
sheet.write(r, c+6, col)
#Updates the database after it has grabbed the needed info to create a contact in salesforce so that it does not get that same data again
workbook.close()
def CreateContact():
# Knows what file to look into for the information to create a contact
path = 'outfile.xlsx'
workbook = xlrd.open_workbook(path, on_demand = True)
sheet = workbook.sheet_by_index(0)
for rowx in xrange(1, sheet.nrows):
try:
Id = sheet.row_values(rowx, start_colx=0, end_colx=None)[0]
Email1 = sheet.row_values(rowx, start_colx=1, end_colx=None)[0]
Newsletter = sheet.row_values(rowx, start_colx=2, end_colx=None)[0]
First = sheet.row_values(rowx, start_colx=3, end_colx=None)[0]
Last = sheet.row_values(rowx, start_colx=4, end_colx=None)[0]
Phone = sheet.row_values(rowx, start_colx=5, end_colx=None)[0]
Street = sheet.row_values(rowx, start_colx=6, end_colx=None)[0]
Apt = sheet.row_values(rowx, start_colx=7, end_colx=None)[0]
City = sheet.row_values(rowx, start_colx=8, end_colx=None)[0]
Postal = sheet.row_values(rowx, start_colx=9, end_colx=None)[0]
d = sf.query("SELECT Id FROM Contact WHERE Phone ='%s' OR Email='%s'" % (Phone, Email1))
# d = returns more than just a case number so data = will specifically only get the case number
data = [e["Id"] for e in d["records"]]
sf.Contact.update(data[0],{'OwnerId' : "005U0000005jwPH", 'Phone' : Phone, 'Email' : Email1, 'Newsletter__c' : Newsletter, 'MailingStreet' : Street+" "+Apt, 'MailingCity' : City, 'MailingPostalCode' : Postal, 'AccountId' : "001U000001ZEB89"})
cursor.execute("UPDATE rma_customer AS RC INNER JOIN rma_customer_address CA ON RC.cus_id = CA.cus_id SET RC.SF_created=1 WHERE RC.cus_id='%s'" % Id)
cursor.execute("UPDATE rma_customer_address AS CA INNER JOIN rma_customer RC ON CA.cus_id = RC.cus_id SET CA.SF_created=1 WHERE CA.cus_id='%s'" % Id)
cursor.execute("UPDATE rma_customer_address AS CA INNER JOIN rma_customer RC ON CA.cus_id = RC.cus_id SET CA.cus_updated=0 WHERE CA.cus_id='%s'" % Id)
cursor.execute("UPDATE rma_customer_details AS RCD INNER JOIN rma_customer RC ON RCD.cus_id = RC.cus_id SET RCD.SF_created=1 WHERE RCD.cus_id='%s'" % Id)
# If Contact exists, skips and goes to next customer in excel sheet.
except Exception, e:
#print "Exception %s"%str(e)
Id = sheet.row_values(rowx, start_colx=0, end_colx=None)[0]
Email1 = sheet.row_values(rowx, start_colx=1, end_colx=None)[0]
Newsletter = sheet.row_values(rowx, start_colx=2, end_colx=None)[0]
First = sheet.row_values(rowx, start_colx=3, end_colx=None)[0]
Last = sheet.row_values(rowx, start_colx=4, end_colx=None)[0]
Phone = sheet.row_values(rowx, start_colx=5, end_colx=None)[0]
Street = sheet.row_values(rowx, start_colx=6, end_colx=None)[0]
Apt = sheet.row_values(rowx, start_colx=7, end_colx=None)[0]
City = sheet.row_values(rowx, start_colx=8, end_colx=None)[0]
Postal = sheet.row_values(rowx, start_colx=9, end_colx=None)[0]
logger.error("Unable to Create Contact: "+Email1+" "+Phone)
logger.error(e)
sf.Contact.create({'FirstName' : First, 'LastName' : Last, 'Newsletter__c' : Newsletter, 'Email' : Email1, 'Phone' : Phone, 'MailingStreet' : Street+" "+Apt, 'MailingCity' : City, 'MailingPostalCode' : Postal, 'AccountId' : "001U000001ZEB89"})
#print "Contact Not Found. Created New Contact!"
cursor.execute("UPDATE rma_customer AS RC INNER JOIN rma_customer_address CA ON RC.cus_id = CA.cus_id SET RC.SF_created=1 WHERE RC.cus_id='%s'" % Id)
cursor.execute("UPDATE rma_customer_address AS CA INNER JOIN rma_customer RC ON CA.cus_id = RC.cus_id SET CA.SF_created=1 WHERE CA.cus_id='%s'" % Id)
cursor.execute("UPDATE rma_customer_address AS CA INNER JOIN rma_customer RC ON CA.cus_id = RC.cus_id SET CA.cus_updated=0 WHERE CA.cus_id='%s'" % Id)
cursor.execute("UPDATE rma_customer_details AS RCD INNER JOIN rma_customer RC ON RCD.cus_id = RC.cus_id SET RCD.SF_created=1 WHERE RCD.cus_id='%s'" % Id)
#time.sleep(10)
# Sleep for 30 seconds to allow Salesforce to catch up before creating the cases to ensure they are correctly associated
workbook.release_resources()
time.sleep(5)
GetNewContact()
CreateContact()