inserting null/nan
Hello,
i'm having problem inserting/upserting np.nan in salesforce.
What I've tried:
- upserting with np.nan -> SALESFORCE MALFORMED QUERY
- replacing np.nan by '' (empty string) -> Salesforce converts those to 0
- replacing to 'null' -> SALESFORCE MALFORMED QUERY
Therefore, how should I do to upsert np.nan ?
Also, how should I do if I want to blank a field (ie inserting null) vs don't want to edit the field ?
I know in the dataloader there is an option to that but how should that work using simple salesforce ?
Thanks
Youll need to replace it with “None”. If you arr going from a dtaframe then run
Dd = Df.where((pd.notnull(df)),None)
Recommending to @nickcatal to close this and document this as the correct answer for assigning a null value to a field with an update operation.
I confirm that setting the field value to None works here.
In my case, I'm converting a pandas DataFrame to a dict, then setting the values of that dict to the python None. So the payload looks like this:
{'salesforce_field_to_update__c': None}
I think setting blank cells to None will set anywhere I have a blank in my Data to blank in Salesforce. Is there a simple way to skip those blank cells instead? Let's say I have a source of data (in a data frame) with holes in it. I want to update anything in my Salesforce with this data unless there is an existing value in SF and no value in my df. Is this easily doable with some sort of option or something?
I'm hoping for something like:
data = [
{'Id': '0000000000AAAAA', 'Email': '[email protected]'},
{'Id': '0000000000BBBBB', 'Email': nan }
]
sf.bulk.Contact.update(data[, skip_na=True])
where the first record would be updated and the second record would not.
I wasn't able to find a way to use bulk for this, but I did find a way to skip the holes in the data. It basically works like this:
data = pd.read_csv('some_data.csv')
results = {}
for row in data.iterrows():
# drop the null values
c = row[1].dropna()
# I also want to drop 0s which count as null in my source data
# and to convert the row to a dict
d = dict(c[c!=0])
conID = d.pop('Id')
if len(d) > 0:
try:
row_res = sf.Contact.update(conID,d)
results[conID] = row_res
except Exception as exc:
results[conID] = exc
else:
results[conID] = 'Nothing to update'
I'd love to find out if this approach helps you out. :) I'd love even more if you can tell me a better way to get this done.
It might be helpful to add an error message for this (that then tells you to use None instead). I got the ValueError: Out of range float values are not JSON compliant. Took me a while to read through the stack trace to find that this was a decision in the simple salesforce package.