The Clone functionality is often used in Salesforce to create records that are similar to each other, reducing unnecessary retyping.

Users can create copies of a sObject both in Classic and in Lightning by using the clone button. What I recently discovered is the different behavior of the clone functionality between Classic and Lightning Experience: in Lightning only the fields showed on the page are copied while in Classic the record is cloned copying all the fields.

If you want to have the same behavior, you need to customize the functionality and I’m going to show how by creating a button which drives Apex/Visualforce code to handle the cloning.


First of all you need to create a custom controller (Opportunity_Controller) whose constructor will query all the fields that must be cloned.
Inside the apex class, you have to create a new method (cloneOpportunity)  in order to add cloning logic; this method will return the Page Reference of the new cloned record.
In the following example we will refer to the Opportunity object.


Subsequently, you need to create a Visualforce Page that calls the action ‘cloneOpportunity’ from the controller; this action returns a page reference that will readdress me to the cloned record.
It is important to check the Security setting of the VF page in order to enable the right profile access for the page.


The last step is to create the custom ‘Clone’ button (Setup -> Object of interest -> Buttons and Links) to render the Visualforce Page.
Now you can click on New Button or Link and enter the following button attributes. Choose the name, click on Detail Page Button bullet and click on Content Sourcesetting Visualforce Page; then choose the Visualforce Page created for the Content and finally add the button to the page layout.

Support us with a small donation:

  • LTC Address: La5f6W1rPr5VHFGCrCmPJ3sSa2AiwKZJbU
  • BTC Address: 1FG1j42MUze8jiW7JbgNaUMZxZeu7M1b4f
  • Ripple Address: rPVMhWBsfF9iMXYj3aAzJVkPDTFNSyWdKy     Tag: 562614972
  • ETH Address: 0x0940958167ca9cbd6409999957a011be7907d904
(Visited 7,465 times, 3 visits today)
Massimiliano Avena
Massimiliano Avena

Massimiliano is a Salesforce Native (Salesforce administrator and developer certified) with a great passion for new digital technologies. His enthusiasm and energy are driving him to love challenges and to deepen the most hidden secrets of Salesforce. He is looking forward to sharing his point of view and the solutions he finds with the Web Community


  1. Hi,

    I’m using your code to clone a case record, but the new case created should redirect to vf page, which helps the users to change the old values or update any values.

    As I already have a action function used, I’m not sure how can I redirect to vf page. Please let me know how can I solve this.




  2. Hi, I am very new to salesforce, follow your steps to created a custom clone button, it’s work perfectly.

    But when I try to deploy into production, it require me to write a test case, would you please provide me a sample code for test case on this scenarios.


  3. How to override clone functionality in lightning since I am not sure whether custom VF page will work.

  4. Is this way works for lightning also? If not can you provide steps and the lightning code, test class also?

Leave a Comment