I have a field on Contact object that is called Test__c. It is Text(18), unique & external Id.
I'm trying to upsert a list of Contacts by the Test__c field. The last Contact on the list has the same value on Test__c as the first one. When I'm trying to upsert list of 199 Contacts the error is thrown:
System.DmlException: Upsert failed. First exception on row 0; first error: DUPLICATE_VALUE, Duplicate external id specified: 1: [Test__c]
When I change number of Contacts to 201, error is not thrown. 200 records are inserted and 1 is updated.
Here is my sample code:
List<Contact> contacts = new List<Contact>();
for(Integer i = 1; i<=199; i++){
contacts.add(
new Contact(
LastName = 'TestName' + i,
Test__c = String.valueOf(i)
)
);
}
contacts.add(
new Contact(
LastName = 'Updated name',
Test__c = '1'
)
);
upsert contacts Test__c;
It looks like 'upsert' processes list by 200 records and then treat another 200 records as another bulk for upsert. Can someone confirm this? I cannot find anything about it in documentation.
(In my project I will be provided a list from integration with another system and I thougt about catching errors during upsert to show it to customer that they have duplicates in their database, but it looks its not possible this way).