[SalesForce] Error when bulk updating Saleforce

I have a dataframe of two columns namely Id and datetimehelp (Holds time stamp value).

Given below is the view of the data in the dataframe labelled account_update_subset

Id,datetimehelp
123,18/05/18 9:50
567,18/05/18 9:45

I am writing the below Python code to have this update to a Salesforce object (Account) but it throws me an error Response content: {'exceptionCode': 'InvalidBatch', 'exceptionMessage': 'Records not processed'}

if len(account_update_subset) > 0:
    key_update_attributes_list = []
    update_attributes_list = []
    for i in range(len(account_update_subset)):
        update_attributes_data = {'Id': account_update_subset['Id'].iloc[i],
                              'datetimehelp': account_update_subset['datetimehelp'].iloc[i]}

        update_attributes_list.append(update_attributes_data)
        sf_data_cursor.bulk.Account.update(update_attributes_list)

Could anyone help me find where am I going wrong in this Python script. Thanks

Best Answer

I'm assuming here that you're using simple_salesforce, because your bulk invocation looks like that API.

It's not immediately clear to me which of these issues causes the specific error you're observing, but I see at least three issues with this code.

  1. You are repeatedly updating the same Accounts, which results in creation of way more of your limited Bulk API jobs than you probably want to use.

    for i in range(len(account_update_subset)):
        update_attributes_data = {'Id': account_update_subset['Id'].iloc[i],
                          'datetimehelp': account_update_subset['datetimehelp'].iloc[i]}
    
        update_attributes_list.append(update_attributes_data)
        sf_data_cursor.bulk.Account.update(update_attributes_list)
    

Note that here, your update() call is inside the for loop where you accumulate updatable Accounts in update_attributes_list. You should move that update() call outside the loop, so that you call it exactly once for the entire list in a single operation. Right now, the first Account in your list is getting updated len(account_update_subset) times.

  1. You appear to be providing an invalid field for update, 'datetimehelp'. If this is a custom field, you'll need to refer to it by the proper API name, such as'datetimehelp__c'.

  2. The date-time values you're providing in dd/mm/yy hh:mm:ss are not a valid format for Salesforce to accept. The valid incoming formats are documented here and depend on whether your target field is Date or DateTime in type:

YYYY-MM-DD
YYYY-MM-DD hh:mm:ss
YYYY-MM-DD hh:mm:ss
YYYY-MM-DDThh:mm:ssZ
YYYY-MM-DDThh:mm:ss.sssZ

Related Topic