+ Start a Discussion
Lakshmi SLakshmi S 

How to clone the record using custom visualforce pages?

Hi Team,

I have created custom detail page using visualforce pages, How to clone the record using Visualforce pages ?

Thanks,
Lakshmi.
Saroja MuruganSaroja Murugan

Please refer the below link,

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

I think its works fine for you.

Thanks,
Saroja
SweetPotatoTec

Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same issue
1) https://help.salesforce.com/articleView?id=000227675&type=1

To accomplish this Task we have to do 3 things. 

(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}";
Note - you can modify the javascript according to the needs of your business use case. 

Disclaimer: This is only a sample code and you can modify and use it on your discretion by testing the use case in your sandbox. 

 
Lakshmi SLakshmi S
Thank you Saroja and Amit for your quick and valuable response.
Lakshmi SLakshmi S
Hi Amit,

I got a issue with below code, If i click on clone the record automatically saved to db, with out clicking the save button.
My requirement is with out clicking the save button i don't want to save the record.
Please check my code and let me know , if any changes required in my code.
public PageReference doClone(){
        try{
            //con.save();
            PageReference pr = Page.HighRiskSuccessFactorsVFPage;
            pr.setRedirect(true);
            High_Risk_Success_Factors__c hr = [SELECT id,name FROM High_Risk_Success_Factors__c where id =:con.getId()];
            High_Risk_Success_Factors__c clonedhr = hr.clone(false,true,false,false);
            insert clonedhr;
            pr.getParameters().put('id',clonedhr.Id);
            return pr;
        }catch(Exception e){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, e.getMessage()));
            return null;
        }
    }
Thanks
lakshmi