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
System Admin 949System Admin 949 

Clone Button Not Working

Hi all,
I am creating the Clone button for Quote object in Lightning.the clone  button is working fine but after saving the record it won't get back to the newly created record.can any one suggest where am i getting wrong. it is redirected fine but creates two records one newly cloned record and one more is again same old record.
for example i have one record 'abc',i want to clone this and given name 'sample' and save the record.in records it will show 3 records.
abc,abc,sample.how to rectify this.i hope you understand.
my sample code is
apex class

/*public class QuoteCloneController {
    
    //added an instance varaible for the standard controller
    private ApexPages.StandardController controller {get; set;}
     // add the instance for the variables being passed by id on the url
    Quote po {get;set;}
    // set the id of the record that is created -- ONLY USED BY THE TEST CLASS
    public ID newRecordId {get;set;}

    // initialize the controller
    public QuoteCloneController(ApexPages.StandardController controller) {

        //initialize the stanrdard controller
        this.controller = controller;
        // load the current record
        po = (Quote)controller.getRecord();

    }

    // method called from the VF's action attribute to clone the po
    public PageReference cloneWithItems() {

         // setup the save point for rollback
         Savepoint sp = Database.setSavepoint();
         Quote newPO;

         try {

              //copy the Quote - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             po = [select Id, Name, AccountId,OpportunityId,ContactId,Sales_Price__c,Payment_Terms__c,Pricebook2Id  from Quote where id = :po.id];
             newPO = po.clone(false);
             insert newPO;

             // set the id of the new po created for testing
             // newRecordId = newPO.id;

             // copy over the line items - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             List<QuoteLineItem> items = new List<QuoteLineItem>();
             for (QuoteLineItem pi : [Select p.QuoteId, p.Product2Id, p.Subtotal, p.ListPrice, p.Quantity,p.UnitPrice,p.PricebookEntryId,p.OpportunityLineItemId From QuoteLineItem p where QuoteId = :po.id]) {
                  QuoteLineItem newPI = pi.clone(false);
                  newPI.QuoteId = newPO.id;
                  items.add(newPI);
             }
             insert items;

         } catch (Exception e){
             // roll everything back in case of error
            Database.rollback(sp);
            ApexPages.addMessages(e);
            return null;
         }

        return new PageReference('/'+newPO.Id+'/e?retURL=%2F'+newPO.Id);
        //return new PageReference('/'+newPO.id+'/e?retURL=%2F');
        
         
        
    }

}*/








public class QuoteCloneController {
    
    //added an instance varaible for the standard controller
    private ApexPages.StandardController controller {get; set;}
     // add the instance for the variables being passed by id on the url
    Quote po {get;set;}
    // set the id of the record that is created -- ONLY USED BY THE TEST CLASS
    public ID newRecordId {get;set;}

    // initialize the controller
    public QuoteCloneController(ApexPages.StandardController controller) {

        //initialize the stanrdard controller
        this.controller = controller;
        // load the current record
        po = (Quote)controller.getRecord();

    }

    // method called from the VF's action attribute to clone the po
     public PageReference cloneWithItems() {

         // setup the save point for rollback
         Savepoint sp = Database.setSavepoint();
         Quote newPO;

         try {

              //copy the Quote - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             po = [select Id, Name, AccountId,OpportunityId,ContactId,Sales_Price__c,Payment_Terms__c,Pricebook2Id  from Quote where id = :po.id];
             newPO = po.clone(false);
             insert newPO;

             // set the id of the new po created for testing
               newRecordId = newPO.id;
         } catch (Exception e){
             // roll everything back in case of error
            System.debug('Error'+e);
            Database.rollback(sp);
            ApexPages.addMessages(e);
            return null;
         }

        //return new PageReference('/'+newPO.id+'/e?retURL=%2F'+newPO.id);
         return new PageReference('/'+newPO.id+'/e?clone=1');

    }

}
vf page
<apex:page standardController="Quote"
     extensions="QuoteCloneController" action="{!cloneWithItems}">
    
     <apex:pageMessages />
</apex:page>
thanks in advance.
Raj VakatiRaj Vakati
I tested the same code but it is not creating duplicate codes .. can you please check is there any triggers or process builders at the backend which is creating the duplicate records ?? 

Or your URL redirect also having an issue ..After i click save on cloned records it's not redirecting to the correct page ..
System Admin 949System Admin 949
No Raj,in my case it created one more record.there is no process and no trigger.in my case the url also reditrected fine.
thanks
System Admin 949System Admin 949
i tried with developer edition and url is
return new PageReference('/'+ newQO.id +'/e?clone=1&retURL=%2F'+ newQO.id);
 i think it is the edition problem or not.i tried with different urls like clone=0,clone=1,clone=2,..etc all the conditions single record is created but not redirected except clone=1.if clone=1 two records created and redirected.one is clone record and again one more  previous record is created.
for example i have one record 'abc',i want to clone this and given name 'sample' and save the record.in records it will show 3 records.
abc,abc,sample.how to rectify this.i hope you understand.
thanks
System Admin 949System Admin 949
again tried with sandbox also getting same result,created new and previous record once again
thanks