+ Start a Discussion
Lakshmi SLakshmi S 

Custom cloning functionality using apex class?

Hi Team,

Requirement : I have created custom vf page for showing detailed record. Form here i have created custom clone button. I want to clone the record from detail page and i need to edit the fields as our own details (not existing record details) and create a new record.
How can we write additional vf page and controller class for cloning functionality.
Note : After clicking on clone button that page redirected to another page and show the record details, if i click on cancel button no need to create new record, until clicking on save button.

Please let me know any one , how can we achieve this functionality........


Thanks in Advance!....
Lakshmi.
 
<Saket><Saket>
Hi Lakshmi,

R u overriding the standard clone button, or it is a custom only?
Lakshmi SLakshmi S
Custom only.
Prem ChauhanPrem Chauhan
Hi Lakshmi S,

Try this below code. it will work for your requirement.
// VISUALFORCE PAGE CODE


<apex:commandLink action="{!cloneAction }"> Clone
                        <apex:param assignTo="{!selectedAccId}" value="{!a.Id}" name="selectedAccId"/>
                        </apex:commandLink> 

//APEX CLASS

  public PageReference cloneAction(){
        Account accRec = [SELECT id, accountNumber, name, type,industry, phone, website,annualRevenue, Number_Of_Opportunities__c FROM Account WHERE id=:selectedAccId];
        Account cloneAcc = accRec.clone(false,false,false,false);
        insert cloneAcc;
        PageReference ref= new PageReference('/'+cloneAcc.Id+'/e?retURL=%2F'+cloneAcc.Id);
        return ref;
    }
Mark it as Best Answer if you find this above code easy.

And I think you should check out this best blog it is really awesome. and you'll get some useful information there. 
http://salesforceupdates.com
do follow this blog for more information about salesforce...