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
Dheeraj shokeenDheeraj shokeen 

How to insert the duplicate record is any way to insert the duplicate record using Apex

Status Code :    500
Error Response :    [{"errorCode":"APEX_ERROR","message":"System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATES_DETECTED, You're creating a duplicate Lead record. We recommend you use an existing record instead.: []\n\nClass.LeadReceiver.doPost: line 13, column 1"}]
SwethaSwetha (Salesforce Developers) 
HI Dheeraj,
By setting the allOrNone parameter of the Database.insert() method to false, you allow the insertion of the duplicate record. However,  this bypasses the duplicate rules entirely and it is generally not recommended to insert duplicate records as it can lead to data inconsistencies and other issues.

Example code
Lead duplicateLead = new Lead(
    FirstName = 'John',
    LastName = 'Doe',
    Company = 'Acme Inc.',
    Email = ''

Database.SaveResult saveResult = Database.insert(duplicateLead, false);

if (saveResult.isSuccess()) {
    // Record was inserted successfully
} else {
    // Check if there were any errors
    if (saveResult.getErrors().size() > 0) {
        for (Database.Error error : saveResult.getErrors()) {
            // Handle the specific error
            System.debug('Error message: ' + error.getMessage());

This would give output as Error message: Use one of these records? which implies the duplicate rule is active and is preventing the creation of duplicates. In such cases, you may need to either update the existing record or modify the duplicate rule (You may check this in Salesforce under Setup > Data Management > Duplicate Management > Duplicate Rules) to allow the insertion of duplicates if that aligns with your business requirements.


If this information helps, please mark the answer as best. Thank you
Dheeraj shokeenDheeraj shokeen
Above code are not working bcz duplicate rules are Standard duplicate rule activated in my Org ,so hwo to insert the duplicate lead
Bryan Leaman 6Bryan Leaman 6
You need to specify a duplicate rule header in the dml options parameter for the insert:

        Database.DMLOptions dmlOptions = new Database.DMLOptions();
        dmlOptions.duplicateRuleHeader.allowSave = true;
        Database.insert(newlead, dmlOptions);