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
Lukesh KarmoreLukesh Karmore 

create duplicate lead when lead is insert trigger

I am getting an error in above scenario.
Error is 
CreateDuplicateLead_WhenLeadIsInserted: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATES_DETECTED, Use one of these records?: [] Trigger.CreateDuplicateLead_WhenLeadIsInserted: line 14, column 1
trigger CreateDuplicateLead_WhenLeadIsInserted on lead (after insert) {
    List<Lead> dupLead = new List<Lead>();  
    for(Lead ld : Trigger.new)
    {
      Lead leadrec = ld.clone(false);
     dupLead.add(leadrec);
           }
    insert  dupLead;
}
Thank You
Suraj Tripathi 47Suraj Tripathi 47

trigger CreateDuplicateLead_WhenLeadIsInserted on lead (after insert) {
if(checkRecursive.runOnce()){
List<Lead> leadList = new List<Lead>();
leadList = trigger.new.deepClone();
insert leadList;
}
}


public Class checkRecursive
{
private static boolean run = true;
public static boolean runOnce()
{
if(run)
{
run=false;
return true;
}
else
{
return run;
}
}
}


Please Mark it as Best Answer if it helps.

Thanks.

Omar Rajab 94Omar Rajab 94
Hi Lukesh, 

Because there ist an active Duplicate Rule for the Lead object. Deactivate this Rule if not conflict the  business requirements. 

Moreover the Trigger should be like this: 
trigger CreateDuplicateLead_WhenLeadIsInserted on lead (after insert) {

 switch on Trigger.OperationType {
        when AFTER_INSERT {
        	if(!checkRecursive.run) {
        	checkRecursive.run = true; 
            List<Lead> dupLead = new List<Lead>();  
		    for(Lead ld : Trigger.new)
		    {
		      Lead leadrec = ld.clone(false);
		      dupLead.add(leadrec);
		     }
		    insert  dupLead;
        	}
          }
      }
}
And this class to aviod the recursion: 
public Class checkRecursive
{
private static boolean run = false;

}

Please mark it as the best answer if it helps you to fix the issue.
Regards, 
Omar