function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
nabhan karminabhan karmi 

System.DmlException: Upsert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found

I had a trigger that fired from webhook, this trigger receive JSON that contain external Id for account object, inside the trigger I do an upsert on account object using the following code
Database.upsert(accountrecord, external_id__c)


but sometimes for unknown reason I got the following exception
System.DmlException: Upsert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: External_Id__c duplicates value on record with id: 0011x00000Gc4LI: []


I tried to add SOQL query to retrieve the account record with the external Id I have to testing purpose and I didn't get any records!
Pranika JindalPranika Jindal
Hello,

1) First of all you cannot retrieve the accounts inserted in test classes.Because they are inserted in test factory and aer used only for testing purpose.
2) For the error, your query is inserting the same records again thats why it is giving duplicate value error.

Let me know if it helps.

Thanks!
nabhan karminabhan karmi
Hi @Pranika Jindal,

Thank you for reply, but I didn't mention that I used test class, I mean I added SOQL query to check if the record is exist or not, and  I use upsert if the record is exist then it should update it, right?