0

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.

0

1 Answer 1

3

As the error message suggests, Salesforce has a notion of "restriction" on a picklist. When a picklist is restricted, input to that field is validated and clamped to the set of picklist values that are already present in Salesforce Setup, in the application's schema. When a picklist is not restricted, you can place any value in that field, whether or not it's a preestablished value.

It appears that field1 is unrestricted and field2 is restricted. If that's the case, you can either

  • Remove the restriction on field2 in Setup->Object Manager
  • Cleanse your data to include only the preexisting picklist values
  • Add all of the picklist values present in your data to the picklist value set.

The right answer will depend on your business requirements, but in general picklists are restricted for a reason and this would suggest to me that the data needs to be cleansed before being put into Salesforce.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .