Maile Cupo
Maile Cupo

Reputation: 748

Salesforce bulk api using the python simplesalesforce package

I am using the simple-salesforce python package with python 3. According to the documentation, I should be able to bulk update records using this syntax:

data = [{'Id': '0000000000AAAAA', 'Email': '[email protected]'}, {'Id':'0000000000BBBBB', 'Email': '[email protected]'}]

sf.bulk.Contact.update(data)

However the instance of simple-salesforce that I implemented does not recognize

sf.bulk.Custom_Object__c

When I try to execute:

sf.bulk.Custom_Object__c.update(data)

I get this error:

AttributeError                            Traceback (most recent call last)
<ipython-input-126-b287358940fc> in <module>()
----> 1 sf.bulk.Custom__bject__c.update(data)

AttributeError: 'SFType' object has no attribute 'Custom_Object__c'

Has anyone else worked with the simple-salesforce python package and run into this issue before?

Upvotes: 4

Views: 3811

Answers (2)

Edu Mar&#237;n
Edu Mar&#237;n

Reputation: 89

You should use Salesforce() function to login instead of SFType(). Here an example:

1. Login

import pandas as pd
from simple_salesforce import Salesforce

sf = Salesforce(username='my_username', password='my_password', security_token = 'my_token', domain = 'login')

2. Data

In case you are using pandas to manage your data, you will need to convert your df to a list of dictionaries using df.to_dict('records') method before the update.

fake_df = pd.DataFrame({'Id': ['rowID1', 'rowID2'], 'CustomField__c':[1,2]})
fake_dict = fake_df.to_dict('records')

3. Update

Although you could use sf.bulk.MyCustomObject__c to set the name of the SFDC object to be updated, I prefer sf.bulk.__getattr__(objectName) so you can reuse the code in a function and easily switch objects.

objectName = 'MyCustomObject__c'
results = sf.bulk.__getattr__(objectName).update(fake_dict, batch_size = 10000)

Upvotes: 1

Jwok
Jwok

Reputation: 722

I use sf.bulk.Custom_Object__c.update(data) regularly and have never encountered this issue. I would suggest trying out the following troubleshooting methods:

  • In Salesforce, go to the object in the setup menu and make sure you are using the correct API Name. Sometimes custom objects' labels differ from their API Names (i.e. if the object label was changed after creation)
  • Confirm that your simple-salesforce initialization code (sf = Salesforce(password=userPassword, username=userName, organizationId=orgId)) is bringing you into the correct org (that your orgId is correct)
  • As a last ditch effort, you could try reinstalling simple-salesforce. I suggest this as normally errors I encounter with this type of command results in a SalesforceMalformedRequest error rather than a Attribution error, and it is possible you are using an out-of-date version of Simple-Salesforce

Upvotes: 0

Related Questions