How do I store simple_salesforce update responses?

518 views Asked by At

I am using simple_salesforce to update records in Salesforce

I would like to keep a "log" of which records were successfully updated and any error messages that were received. Ex: The response for a successful update is "204". How do I save that response to a variable?

Once I have that value in a variable I know how to store it in a log table. Thanks so much for your help

sf.table.update(uniqueID,{fieldname: new_AccountID}) 
1

There are 1 answers

0
Pavel Slepiankou On

Hope I got the question correct because the answer is - just assign the result to a variable

from simple_salesforce import Salesforce, SalesforceResourceNotFound, SalesforceMalformedRequest

id = '0030e0000000001'
try:
    update_result = salesforce.Contact.update(id, {'Phone': '+1234567890'})
    # alternatively you can create log line just here 
    # update_result = f'{id} was successfully updated'
except SalesforceResourceNotFound:
    update_result = f'record {id} not found'
except SalesforceMalformedRequest as ex:
    print(ex)
    
print(update_result)
#$> 204 or 'record 0030e0000000001aaaaaaaaa not found' on exception when id is incorrect

Although if you're dealing with multiple records you have to consider using bulk api

contact_data = [
    {'Id': '0030e0000000001', 'Phone': '11111111111', 'Custom_Field__c': 1000},
    {'Id': '0030e0000000002', 'Phone': '22222222222', 'Custom_Field__c': 2000}
]

update_results = salesforce.bulk.Contact.update(contact_data)

and then the update_results looks like that

[
    {'success': True, 'created': False, 'id': '0030e0000000001', 'errors': []},
    {'success': True, 'created': False, 'id': '0030e0000000002', 'errors': []}
]