2

I am trying to load bulk data from csv file to Salesforce using python. when i am trying to create single record in Salesforce it's working fine

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'})

but i want to load bulk data

2 Answers2

2

Below is a sample of the code I use when doing a bulk create of a custom object:

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")
theurlin
  • 181
  • 1
  • 1
  • 8
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). You can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question once you have enough [reputation](https://stackoverflow.com/help/whats-reputation). - [From Review](/review/low-quality-posts/18603000) – EJoshuaS - Stand with Ukraine Jan 23 '18 at 16:33
  • 1
    My code shows an example of how to "load bulk data from csv file to Salesforce using python." – theurlin Jan 23 '18 at 17:38
0

Take a look at different python library SQLForce

o-lexi
  • 103
  • 2