0

I have try to clone Opportunity records and done till 3 fix field But I need for all fields.But without using clone() method.

Code -

public class Cloningwithout {
public static void insertclone(){
    Opportunity opp = new Opportunity(Name='Opportunity2',  CloseDate=date.parse('06/07/2012'),StageName='Prospecting');
    insert opp;
    
   ID Oppid =opp.ID;

    clone1(Oppid);
    
    
}
public static void clone1(String IDs){
    Opportunity sourceopp = [SELECT Id,Name,CloseDate,StageName from Opportunity Where Id=:IDs];
    Opportunity targertopp = new Opportunity();
    targertopp.Name = sourceopp.Name;
    targertopp.CloseDate = sourceopp.CloseDate;
    targertopp.StageName = sourceopp.StageName;
    
    insert targertopp;
} }

1 Answers1

0

maximum trigger depth exceeded

Your trigger runs after Opportunity is inserted. It inserts new opportunity, which causes the trigger to run, which inserts new opportunity... Until about 16 recursive operations, then SF terminates your action. Using clone() or not has nothing to do with it.

What are you trying to do, insert just once and then skip the trigger? You could have helper checkbox on Opportunity or a static variable

trigger CloneTrigger on Opportunity (after insert) {
static Boolean skip = false;

if(!skip){
    skip = true;
    Opportunity[] ls=new Opportunity[]{};
    for(Opportunity o: trigger.new){
        Opportunity obj=new Opportunity();
        
       // obj.id = null;
        obj.name=o.name;
        obj.CloseDate = o.CloseDate;
        obj.StageName = o.StageName;
        
        ls.add(obj);
    }
    insert ls;
}

}
eyescream
  • 18,088
  • 2
  • 34
  • 46