+ Start a Discussion
Harshal Patil 15Harshal Patil 15 

How to Write a Trigger on Lead which will create the clone record. (Hint : Map trigger.new to clone record)

Best Answer chosen by Harshal Patil 15
Khan AnasKhan Anas (Salesforce Developers) 
Hi Harshal,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Trigger:
trigger LeadClone on Lead (after insert) {
    
    if(CheckRecursive.runOnce()) {
        List<Lead> leads = new List<Lead>();
        for (Lead record : trigger.new){
            leads.add(record.clone(false, false, false, false));
        }
        if(leads.size()>0){
            INSERT leads;
        }
    }
}

Helper Class To Stop Recursion:
public Class CheckRecursive {
    
    private static boolean run = true;
    
    public static boolean runOnce() {
        if(run) {
            run=false;
            return true;
        }
        else {
            return run;
        }
    }
}

You need a helper class to stop the recursion. A recursive trigger is one that is called over and over. It can lead to the infinite loop and which can result to governor limit sometime. Sometimes it can also result in unexpected output. If not controlled will result in this error: maximum trigger depth exceeded. So we should write code in such a way that it does not result to recursion.

Or you can use the below trigger:
trigger LeadClone on Lead (after insert) {
   
   if(checkRecursive.runOnce()) {
     List<Lead> leads = new List<Lead>();   
     leads = Trigger.new.deepClone();
     INSERT leads; 
   }
}

Difference between Clone and DeepClone: https://www.biswajeetsamal.com/blog/difference-between-clone-and-deepclone-in-apex-in-salesforce/

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Harshal,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Trigger:
trigger LeadClone on Lead (after insert) {
    
    if(CheckRecursive.runOnce()) {
        List<Lead> leads = new List<Lead>();
        for (Lead record : trigger.new){
            leads.add(record.clone(false, false, false, false));
        }
        if(leads.size()>0){
            INSERT leads;
        }
    }
}

Helper Class To Stop Recursion:
public Class CheckRecursive {
    
    private static boolean run = true;
    
    public static boolean runOnce() {
        if(run) {
            run=false;
            return true;
        }
        else {
            return run;
        }
    }
}

You need a helper class to stop the recursion. A recursive trigger is one that is called over and over. It can lead to the infinite loop and which can result to governor limit sometime. Sometimes it can also result in unexpected output. If not controlled will result in this error: maximum trigger depth exceeded. So we should write code in such a way that it does not result to recursion.

Or you can use the below trigger:
trigger LeadClone on Lead (after insert) {
   
   if(checkRecursive.runOnce()) {
     List<Lead> leads = new List<Lead>();   
     leads = Trigger.new.deepClone();
     INSERT leads; 
   }
}

Difference between Clone and DeepClone: https://www.biswajeetsamal.com/blog/difference-between-clone-and-deepclone-in-apex-in-salesforce/

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Harshal Patil 15Harshal Patil 15
Thnq u :)
Harshal Patil 15Harshal Patil 15
Please Help me with this,
if it is possible..

Trigger for Updating Cases that come in from the Web, Email or Chtt
Use Case
we would like to use the email address of the i ncoming case to see if we can associate the
correct person account to populate the Account and Contact Fields.
When a new case is created and the Case.Origin field is set to “Chat” or “Email” or “Web” take the
Case.SuppliedEmail field and look up to find a match in the following account fields
Account. PersonEmail, Account.Email_Address__c