+ Start a Discussion
Pranav ChitransPranav Chitrans 

Trigger To create Duplicate Lead

I wan to write trigger in such a way that when ever a new lead is created, with the same name i want to create another lead... In short.. I want to create the clone of lead... when new lead is getting created... plz help..
Best Answer chosen by Pranav Chitrans
Ketankumar PatelKetankumar Patel
Hi Pranav, try this trigger code, you still need a helper class to stop recursion.
trigger createDuplicateLead 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;
    }
  }
}

All Answers

James LoghryJames Loghry
Create a Duplicate Lead?  That's a new one!  There's probably a better solution than creating a duplicate lead, such as maybe defining a record type with various page layouts for profiles, etc.

That being said, if you use a Process instead of a Trigger, this is really simple.  Create a process by going to Setup->Create->Workflow and Approvals->Process Builder.  After you create a new process, add the Lead as the object and run the process  when the record is created. Set the criteria to always execute if you need, and then add a Record Create action to create the Lead record.  Map the fields you need.  Save and Activate the process.  Then test it.
Pranav ChitransPranav Chitrans
hey #James Loghry,
In process builder.. how we can get the old record value from recent inserted data.. like if i create the lead withn lastName XYZ and Company is Twitter... then the New lead should have the same value... like XYZ as lastName and Twitter as company name and so on.....
James LoghryJames Loghry
In the Create a Record action / Set Object variables section, change the Type dropdown from String to Reference.  This will allow you to select fields off of the existing lead and map them to the new lead.
Ketankumar PatelKetankumar Patel
Hi Pranav, try this trigger code, you still need a helper class to stop recursion.
trigger createDuplicateLead 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;
    }
  }
}
This was selected as the best answer
Pranav ChitransPranav Chitrans

hey Ketankumar Patel,
I tried your above code but it does not create any duplicate lead with same name and company.... 
Ketankumar PatelKetankumar Patel
Pranav, 

Did you try to update your existing lead or did you try to create brand new lead?

I tested above code. Whenever I create new lead it automatically copy all lead fields and create duplicate lead with all same information.
Pranav ChitransPranav Chitrans
Thanks.... Acutually when i clicked on recently created.. after that it is visible to me, Firstly it was selected by default to recently viewed... but after changing to recently created I can see the duplicate lead....