I am using the salesforce API and struggling to figure out when (and how best?) to set picklist values for different fields.
Here is a code example:
from simple_salesforce import Salesforce, SalesforceLogin, SFType
import pandas as pd
class MyObject:
def __init__(self, Name, field1, field2):
self.Name = Name
self.field1 = field1
self.field2 = field2
def create_salesforce_data_record(self):
property_data = {
'Name': self.Name,
'field1__c': self.field1,
'field2__c': self.field2
}
return property_data
if __name__ == '__main__':
#create connection to salesforce database
session_id, instance = SalesforceLogin(username=my_username, password=my_password, security_token=my_security_token, domain='login')
my_data = pd.read_csv('path_to_csv')
my_data_values = my_data.values.tolist()
for i in my_data_values:
Name = i[0]
field1 = i[1]
field2 = i[2]
new_object = MyObject(Name, field1, field2)
record = MyObject.create_salesforce_data_record(new_object)
add_property = SFType('My_Object__c', session_id, instance)
response = add_property.create(record)
I created a new custom object in salesforce called My_Object__c
. I set the data type for field1
and field2
as 'picklist'.
I am getting an error on field2
:
SalesforceMalformedRequest: Malformed request https://my-boonerealestate.my.salesforce.com/services/data/v42.0/sobjects/My_Object__c/. Response content: [{'message': 'sourceagnt: bad value for restricted picklist field: Picklist Value 1', 'errorCode': 'INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST', 'fields': ['field2']}]
What I don't understand is Why? As I said, both field1
and field2
are picklists. Field1
is handled nicely. All the unique values in that field are ingested and turned into picklist values. The same should happen for field2
. Do I need to set the picklist values before creating new records? This doesn't make sense because I didn't have to do it for field1
.