+ Start a Discussion
System Admin 949System Admin 949 

Custom Clone button using Apex

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.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');
    }

}
vf page
<apex:page standardController="Quote"
     extensions="QuoteCloneController"
     action="{!cloneWithItems}">
     <apex:pageMessages />
</apex:page>

thanks in advance
Sampath SuranjiSampath Suranji
Hi,

Try redirect as below,
 return new PageReference('/'+newPO.id);

regards
Raj VakatiRaj Vakati
Your code looks good .. But i belive you code is failing with some exception 

Can you do this steps 

Add System.debug logs in catch block section and try 

Use this code
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
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);
    }

}

 
System Admin 949System Admin 949
Hi Raj,
while using this line 'return new PageReference('/'+newPO.id);' record created automatically.i tried with this line already.i want to change the values while cloning and save the record,redirected to newly created record detail page.
thanks
Raj VakatiRaj Vakati
Got it .. 

Try this it will work 
 
   return new PageReference('/'+newPO.id+'/e?clone=1');

 
System Admin 949System Admin 949
Hi Raj. it is redirected fine but creates two records one newly cloned record and 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.
thanks