1

So when using Simple_Salesforce (has been working fine for quite some time) but recently made changes so that it would check if Contact exists first and if exist then use sf.Contact.update instead of create but end up with a DUPLICATES DETECTED error and does not allow me to do any update. If I test with my Contact account it works because there are no two same fields for that Contact. But testing on another Contact, that happens to have the same phone number as a couple of other Contacts, it gives the duplicate error even though the email addresses are different. The Duplicate Rule I believe is currently using Fuzzy instead of Exact.

All I want my python script to do is ignore the Duplicate error and just proceed with the update rather than just skipping it altogether.

for rowx in xrange(1, sheet.nrows):
    try:
        Id = sheet.row_values(rowx, start_colx=0, end_colx=None)[0]
        Email1 = sheet.row_values(rowx, start_colx=1, end_colx=None)[0]
        Newsletter = sheet.row_values(rowx, start_colx=2, end_colx=None)[0]
        First = sheet.row_values(rowx, start_colx=3, end_colx=None)[0]
        Last = sheet.row_values(rowx, start_colx=4, end_colx=None)[0]
        Phone = sheet.row_values(rowx, start_colx=5, end_colx=None)[0]
        Street = sheet.row_values(rowx, start_colx=6, end_colx=None)[0]
        City = sheet.row_values(rowx, start_colx=7, end_colx=None)[0]
        Postal = sheet.row_values(rowx, start_colx=8, end_colx=None)[0]
        sf.Contact.create({'FirstName' : First, 'LastName' : Last, 'Newsletter__c' : Newsletter, 'Email' : Email1, 'Phone' : Phone, 'MailingStreet' : Street, 'MailingCity' : City, 'MailingPostalCode' : Postal, 'AccountId' : "001U000001ZEB89"})
        print "Contact Not Found. Created New Contact!"
    except {u'errorCode': u'DUPLICATES_DETECTED'} as error:
        d = sf.query("SELECT Id FROM Contact WHERE Email ='%s'" % Email1)
        # d = returns more than just a case number so data = will specifically only get the case number 
        data = [e["Id"] for e in d["records"]]
        sf.Contact.update(data[0],{'Phone' : Phone, 'Newsletter__c' : Newsletter, 'MailingStreet' : Street, 'MailingCity' : City, 'MailingPostalCode' : Postal})
        # Creates contact
        print "Contact Exists. Contact Info Updated"
        #time.sleep(10)
        print (error)

The Error is:

simple_salesforce.api.SalesforceMalformedRequest: Malformed request https://na48.salesforce.com/services/data/v37.0/sobjects/Contact/. Response content: [{u'errorCode': u'DUPLICATES_DETECTED', u'fields': [], u'message': u"You're creating a duplicate record. We recommend you use an existing record instead."}]

0

The exception handling needs to catch SalesforceMalformedRequest, not a dict containing the error code. Can you try except SalesforceMalformedRequest as error: instead?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.