0

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

  • You either need a Salesforce Id or use any field as external Id to mark them as an identified for the upsert operation. Without any Id, Salesforce will simply create it. – Raul Sep 20 '17 at 12:33
  • If you've already inserted them, you should have the ids of the records. Rather than doing an upsert, you should be doing an update to them and specifying the Id of each record. From the error message you're receiving, your import tool doesn't support upsert since or you've not specified the external Id (email will usually do) since the error message shows it as an Insert. – crmprogdev Sep 20 '17 at 12:38
  • How do i specify a external id? – vlad Sep 20 '17 at 12:42
  • @vlad you create a custom field and check the External Id attribute. Once you do that, specify that field in your upsert call: upsert allContacts Contact.ExtId__c; – sfdcfox Sep 20 '17 at 12:55
2

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.

  • After some code updates i added external ID to the object and created custom field in the Contact standart object and it stil fails with: First exception on row 0; first error: DUPLICATES_DETECTED, Use one of these records?: [] Code: contact.object__c = record.object__c; upsert allContacts Contact.object__c; object__c is GUID so it cant be not unique..... – vlad Sep 20 '17 at 13:43
  • @vlad you'll need to populate that field for all existing records in the database before you can use it, because otherwise your actions will still be considered inserts. – sfdcfox Sep 20 '17 at 13:45
  • and how do i populate that field in org with thousants of old records ..... thats just stupid.. tnx for the help – vlad Sep 20 '17 at 13:50
  • @vlad Use the data loader, import wizard, or you can even write an Apex Code script. – sfdcfox Sep 20 '17 at 14:05

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.