如何使用simple_salesforce模块

时间:2016-11-22 21:23:37

标签: python python-2.7 python-3.x salesforce beatbox

我正在尝试使用python将 csv 文件中的批量数据加载到Salesforce。 当我试图在Salesforce中创建单个记录时,它工作正常

from simple_salesforce import Salesforce
from simple_salesforce import SFType
sfdc = Salesforce(username='username', password='pass', security_token='s_token')
sfdc.testing__c.create({'tt__c': 'name', 'ttmobile__c': '0000000015', 'type': 'testing__c'})

但我想加载批量数据

2 个答案:

答案 0 :(得分:0)

看看不同的python库SQLForce

答案 1 :(得分:0)

以下是我在批量创建自定义对象时使用的代码示例:

from simple_salesforce import Salesforce
import requests
import pandas as pd
import datetime
import json

"""Login Credentials"""
sf_username = "Email Address"
sf_password = "Password"
sf_instanceurl = "https://{Enter Instance}.salesforce.com"
sf_sectoken = "Security Token"
session = requests.Session()


sf = Salesforce(username=sf_username, 
                password=sf_password,
                instance_url=sf_instanceurl,
                session=session,
                security_token=sf_sectoken)

upload_csv = pd.read_csv("Management_Fees.csv")

"""JSON Date Converter"""
def DateConverter(o):
    if isinstance(o, datetime.date):
        return o.__str__()

"""Account Value Fee Function"""
def accountvalue_fee():
    sf.Management_Fee__c.create({"Household_Entity__c":Entity,
                                 "Account_Number__c":Account,
                                 "Billing_Date__c":json.dumps(BillingDate, default=DateConverter).replace('"',""),
                                 "Fee_Type__c":FeeType,
                                 "Billable_Assets__c":BillableAssets,
                                 "Cash_Non_Billable_Value__c":CashNonBill,
                                 "Household_Assets__c":HouseholdAssets})

"""Upload a Management Fee for each row of DataFrame"""
for row in upload_csv.itertuples():  
    try:
        Entity = str(row[12])
        Account = str(row[11])
        BillingDate = datetime.datetime.strptime(str(row[3]), '%m/%d/%Y').date()
        if type(row[5]) is float:
            CashFlowDate = ""
        else:
            CashFlowDate = datetime.datetime.strptime(str(row[5]), '%m/%d/%Y').date()
        BillableAssets = float(str(row[6]).replace(",",""))
        CashNonBill = float(str(row[7]).replace(",",""))
        HouseholdAssets = float(str(row[9]).replace(",",""))
        FeeType = str(row[4])
        accountvalue_fee()
        print(Account + " Done")
    except:
        print(Account + " Upload Failed")