Salesforce: Clone functionality in Classic and in Lightning Experience

on

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.

CREATE A CUSTOM CONTROLLER

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.

CREATE A VISUALFORCE PAGE

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.

CREATE THE BUTTON

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 Source setting Visualforce Page; then choose the Visualforce Page created for the Content and finally add the button to the page layout.
(Visited 2,935 times, 1 visits today)

5 thoughts on “Salesforce: Clone functionality in Classic and in Lightning Experience

  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.

     

    Thanks,

    SS

  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.

    Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *