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
MGargMGarg 

custom clone button

I have created a custom clone button for cloning contacts.

 

When i click on the clone button it takes me to the edit page. Till this point everything is fine.

 

But even if i click on the cancel button, on the edit page, it creates the cloned contact.

 

It should only create the record on clicking the save button. Here is the code

 

public PageReference createContact(){

String errorMessage = '';

//if contact record type is Employee display error message

 if(sourceContact.RecordType.Name == EMPLOYEE){

errorMessage = ' Employee contacts cannot be cloned.'; ApexPages.addMessage(

new ApexPages.Message(ApexPages.Severity.ERROR, errorMessage));

}

//if contact record type is Contact create clone of the contact and return to the edit page of newly created contact

 else if(sourceContact.RecordType.Name == CONTACT){

Contact clonedContact = sourceContact.clone(false, true);

try{

insert clonedContact;}

catch(Exception ex){

errorMessage =' This contact can not be cloned. '+ex; ApexPages.addMessage(

new ApexPages.Message(ApexPages.Severity.ERROR, errorMessage));

}

PageReference pageRef = new PageReference('/' + clonedContact.Id+ '/e?retURL=%2F' + clonedContact.Id);

pageRef.setRedirect(true);

return pageRef;

}

//if page has error messages then display the messages

 if(ApexPages.hasMessages()){

showError=true;

showNoError=false;

return null;

}

return null;

}

SteveBowerSteveBower

 If you think about your code, once you do the "Insert", the new record is there.  Now, after you have the record created, you're simply redirecting the user to the Edit page for that *already existing* record.  When the user hits Cancel, all they are doing is cancelling the Edit operation.  The clone has already occurred.

 

Can you just remove the Clone button from the page layout for the Employee Recordtype?

 

Best, Steve.

MSVRadMSVRad

Check out this link: http://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/

 

He has created a clone override where there is a copy and a save portion - I think.

 

Here is his Controller code:

 

public class PurchaseOrderCloneWithItemsController { //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 private Purchase_Order__c 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 PurchaseOrderCloneWithItemsController(ApexPages.StandardController controller) { //initialize the stanrdard controller this.controller = controller; // load the current record po = (Purchase_Order__c)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(); Purchase_Order__c newPO; try { //copy the purchase order - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE po = [select Id, Name, Ship_To__c, PO_Number__c, Supplier__c, Supplier_Contact__c, Date_Needed__c, Status__c, Type_of_Purchase__c, Terms__c, Shipping__c, Discount__c from Purchase_Order__c 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<Purchased_Item__c> items = new List<Purchased_Item__c>(); for (Purchased_Item__c pi : [Select p.Id, p.Unit_Price__c, p.Quantity__c, p.Memo__c, p.Description__c From Purchased_Item__c p where Purchase_Order__c = :po.id]) { Purchased_Item__c newPI = pi.clone(false); newPI.Purchase_Order__c = 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); } }

 

Hope this is helpful I found it while googling about overriding the clone button.

nishanth0208nishanth0208

Hi, 

 

   I need the Clone button functionality for VisualForce page developed for custom object. I am working on Professional edition. Can i get it anyways?Please let me know.

 

 Thanks in advance.

steve456steve456

If you got the solution for this could you please post it.I am having the same issue