[SalesForce] Upsert failed – DUPLICATES_DETECTED

I am trying to import Contacts from external source abd im getting that error when i am inserting them into Database. The first time i insert them, there is no error, then if i try to insert them(the same) or any of them with any changes, i get an error:

Upsert failed. First exception on row 0; first error:
DUPLICATES_DETECTED, Use one of these records?: []

Thats the code i am using.

    public void InsertContacts(List<SomeObject> contacts) {
    Contact[] allContacts = new List<Contact>();
     for (SomeObject record : contacts){
     Contact contact = new Contact();
      contact.FirstName = record.FirstName;
      contact.Phone = record.Phone;
      contact.Title = record.Title;
      contact.Fax = record.Fax;
      contact.Birthdate = Date.valueOf(record.Birthdate);
      contact.Email = record.Email;
      contact.MailingCity = record.City1;
      contact.MailingState = record.State1;
      contact.MailingPostalCode = record.PostalCode;
      contact.MailingCountry = record.Country1;
      contact.OtherCity = record.City2;
      contact.OtherState = record.State2;
      contact.OtherPostalCode = record.PostalCode2;
      contact.OtherCountry = record.Country2;
      contact.LastName = record.LastName;

      allContacts.add(contact);
      }
    upsert allContacts;
}

Where i am going wrong? Isnt upsert supposed to track if they are the same record and just update them if true? Is there any way to manually add recordId like contact.Id = new Id(); or something like that?

Thats the error i get into dev console:

15:08:33:200 DUPLICATE_DETECTION_MATCH_INVOCATION_DETAILS
EntityType:Contact|ActionTaken:Allow_[Alert,Report]|DuplicateRecordIds:0030Y00000bfmne,0030Y00000bfmnf

as well as

15:08:33:114 DUPLICATE_DETECTION_RULE_INVOCATION
DuplicateRuleId:0Bm0Y000004FwDP|DuplicateRuleName:Standard Contact
Duplicate Rule|DmlType:INSERT

Best Answer

Upsert needs a unique identifier in order to upsert correctly. If you don't specify a field, it defaults to Id. Since you apparently don't have an Id to use, you can either create your own, or you could try using Email:

public void InsertContacts(List<SomeObject> contacts) {
    Contact[] allContacts = new List<Contact>();
     for (SomeObject record : contacts){
     Contact contact = new Contact();
      contact.FirstName = record.FirstName;
      contact.Phone = record.Phone;
      contact.Title = record.Title;
      contact.Fax = record.Fax;
      contact.Birthdate = Date.valueOf(record.Birthdate);
      contact.Email = record.Email;
      contact.MailingCity = record.City1;
      contact.MailingState = record.State1;
      contact.MailingPostalCode = record.PostalCode;
      contact.MailingCountry = record.Country1;
      contact.OtherCity = record.City2;
      contact.OtherState = record.State2;
      contact.OtherPostalCode = record.PostalCode2;
      contact.OtherCountry = record.Country2;
      contact.LastName = record.LastName;

      allContacts.add(contact);
      }
    upsert allContacts Contact.Email;
}

This will, of course, only work if your emails are reasonably unique and not null. If you choose to use a custom field as the External Id, make sure you check the External Id attribute on the field while creating it.

Related Topic