Dates are tricky are in Salesforce and in this post I will walk you through how to set a date to Blank or Null using the Simple Salesforce Python Package.
This post comes from spending way too long dealing with a Malformned query error when trying to bulk update a date field using the Salesforce API. I pinpointed the issue to date fields where I was trying to set them to blank by using a blank string using with quotes''
. Unlike numerical or text fields, date fields are tricky in Salesforce.
To set a date field to blank or null in Salesforce, you have to set the date field to Pandas None.
In the code snippet below, I am converting a Pandas DataFrame date column to a string and setting any rows without a date to the Pandas None.
df['date_column'].apply(lambda x: x.strftime('%Y-%m-%d') if pd.notnull(x) else None)
Once you do that, you can convert the DataFrame to JSON to upload.
#convert the dataframe to json results_json = df.to_json(orient='records',date_format='iso') #convert string to file object contacts_to_update = json.loads(results_json) #client.bulk.update('Contact',contacts_to_update) sf.bulk.Contact.update(contacts_to_update,batch_size=10000,use_serial=True)
For reference, the actual JSON will just show None, without any quotes.
('Last_Login__c', None),
And that’s it! Happy uploading!