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
Puspendra SinghPuspendra Singh 

Duplicate_Detected Error while inserting the contacts in salesforce

Hi I am pulling data from an external source via a webservice callout. My Contacts are duplicate but i am filtering out duplicate contacts using email before the DML operation, but i am still getting below error

"00:01:06:426 EXCEPTION_THROWN [331]|System.DmlException: Insert failed. First exception on row 2755; first error: DUPLICATES_DETECTED, You're creating a duplicate record. We recommend you use an existing record instead.: []"

Can someone please help how do i get rid of this issue?
Best Answer chosen by Puspendra Singh
Ravi Dutt SharmaRavi Dutt Sharma
Hi Pushpendra,

Can you please check your duplicate rule and let us know on which fields it is being configured. Is it only being configured on Email? 
Another question - How are you filtering the duplicate contacts before performing DML? Are you checking the records against the records which are already present in DB? Or are you just filtering the list that you are inserting in your code?
If you have the requirement of always skipping the duplicate rule when you are doing the insertion by pulling the data from external source, then you can make use of below code:
 
Database.DMLOptions dml = new Database.DMLOptions();
dml.DuplicateRuleHeader.allowSave = true;
Database.SaveResult sr = Database.insert(<Pass your contact list here>, dml);

 

All Answers

Ravi Dutt SharmaRavi Dutt Sharma
Hi Pushpendra,

Can you please check your duplicate rule and let us know on which fields it is being configured. Is it only being configured on Email? 
Another question - How are you filtering the duplicate contacts before performing DML? Are you checking the records against the records which are already present in DB? Or are you just filtering the list that you are inserting in your code?
If you have the requirement of always skipping the duplicate rule when you are doing the insertion by pulling the data from external source, then you can make use of below code:
 
Database.DMLOptions dml = new Database.DMLOptions();
dml.DuplicateRuleHeader.allowSave = true;
Database.SaveResult sr = Database.insert(<Pass your contact list here>, dml);

 
This was selected as the best answer
Puspendra SinghPuspendra Singh
Hi Ravi,
Below is my duplicate rule detail. I am not sure how do we check what field it is on. 
For your second question, I am checking database for already available contacts based on email and if only it is not there then only i am creating my list of records that I need to insert. Even after creating this List, i am sorting it and filtering it to make sure there are no records with same email addresses, my list do not have any duplicate email address records.

Duplicate Rule Detail
Ravi Dutt SharmaRavi Dutt Sharma
If you have the requirement of always bypassing the duplicate rule when you are inserting records through apex, then simply use the code I gave in the last comment. If you use this, you do not need to deduplicate the contacts on your side. It will save a lot of efforts on your end and also improve the performance of the code.
Puspendra SinghPuspendra Singh
Thanks Ravi! Yes that solved my problem.