Loading

Clone a Salesforce Record with Limited Fields (Classic and Lightning Experience)

Date de publication: May 22, 2026
Description

With the standard Clone button in Salesforce, every field on a record is cloned. By creating a custom clone solution, you can clone only the specific fields required by your business, excluding fields that should not be copied.

Salesforce Classic: This approach uses a custom Visualforce (VF) page with a standard controller and Apex extension. The VF page references only the fields you want to clone, so the Apex extension clones only those fields when inserting the new record.

 

Lightning Experience: The standard Clone button in Lightning cannot be overridden using Visualforce. For Lightning, the recommended approach is to build a Lightning Web Component (LWC) or Aura component that clones the record and displays only the desired fields. The component is invoked via a Quick Action on the record page. See the Trailhead module on JavaScript Button Alternatives for guidance on migrating Classic custom buttons to Lightning.


 

Résolution

How the Classic Custom Clone Solution Works

This solution is demonstrated using the Lead object. The Visualforce page uses a standard Lead controller and a custom Apex extension. When the custom button is clicked on a Lead record detail page, it opens the VF page. The Apex extension constructor retrieves the current Lead record, and only the fields referenced in the VF page markup are included in the clone. Clicking the Save Clone button inserts the new Lead record with only those selected fields populated.
Pay special attention to required fields — they must be included in the VF page to avoid insert errors.

Step 1: Create the Visualforce Page

Create a Visualforce page using the Lead standard controller and the custom Apex class as an extension. Reference only the fields you want to clone in the page markup using apex:inputField. Add a command button that calls the clone action method.
The page uses apex:pageBlock and apex:pageBlockSection for layout, and includes an apex:commandButton bound to the clonelead action in the controller. Only the fields declared as apex:inputField elements on this page are included in the cloned record.

Step 2: Create the Apex Extension Class

Create a public Apex class named customcloneLead. The class constructor accepts an ApexPages.StandardController parameter and retrieves the current Lead record using std.getRecord(). The clonelead() method creates a new Lead instance, calls the .clone() method on the retrieved record, and inserts the new Lead using a DML insert statement.

Step 3: Create the Custom Button

Create a custom detail page button on the Lead object. Use an OnClick JavaScript button type and set the button action to navigate to the Visualforce page, passing the current record's ID as a URL parameter (e.g., /apex/customcloneLead?id={!Lead.Id}). Add the button to the Lead page layout.

Disclaimer: This is a sample implementation. Test thoroughly in a sandbox environment before deploying to production, and modify the code to meet your specific business requirements.
Numéro d’article de la base de connaissances

000386351

 
Chargement
Salesforce Help | Article