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
adi salesforceadi salesforce 

How to write a vfpage to clone a record

Khan AnasKhan Anas (Salesforce Developers) 
Hi Adi,

I trust you are doing very well.

Please refer to the below links which contains sample code which might help you further with the above issue.

https://salesforceradical.blogspot.com/p/cloning-records-using-clone-method.html

https://help.salesforce.com/articleView?id=000227675&type=1

https://developer.salesforce.com/forums/?id=906F0000000BWMkIAO


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas
Raj VakatiRaj Vakati
Why do you need VF page ?? You can use standard clone or Visual flows or quick actions 
Ajay K DubediAjay K Dubedi
Hi Adi,

Below code can fulfill your requirements. Hope this will work for you.

(1) Create a VF page with standard controller as lead and apex class as extension. 
===================================================================
<apex:page standardController="lead" extensions="customcloneLead">
<apex:form >
<apex:pageBlock title="Lead details">
<apex:pageBlockSection title="My Content Section" columns="2">
<apex:inputField value="{!lead.company}" />
<apex:inputfield value="{!lead.firstname}"/>
<apex:inputfield value="{!lead.lastname}"/>
</apex:pageBlockSection>
<apex:pageBlockButtons>
<apex:commandButton action="{!clonelead}" value="save clone"/>
</apex:pageBlockButtons>
        </apex:pageBlock>
</apex:form>
</apex:page>

(2) Create a apex extension class. 
=============================
public class customcloneLead{
private lead l;
public customcloneLead(apexpages.standardcontroller std){
this.l = (lead)std.getrecord();
}

public void clonelead(){
lead l1 = new lead();
l1 =l.clone();
insert l1;
}
}


(3) Create a custom button on the Lead object which can be an onclick javascript button depending on your needs. Add that button in the layout. 
Sample Javascript Button:
=====================
window.location.href = "/apex/customcloneLead?id={!Lead.Id}";

Please mark this as best answer if this solves your problem.

Thank you
Ajay Dubedi