I have an object whose standard new, edit and view button are overridden with a vf page.I want a Clone button on this vf page which should work exactly like standard clone button. If suppose i open an existing record and click on this clone button on vf page it should load the vf page again and should prepopulate all the field of the old record. I should be able to change the field values and when i click on the save button(already present on the vf page) a new record should be generated. How can i achieve this clone functionality ?
4 Answers
Add a custom button that redirects to /RecordId/e?clone=1
This opens the record in Edit Mode as a clone of the original record.
Clone page has to be customized VF or standard?
For standard you can probably just use /idOfTheOldRecord/e?clone=1
.
For custom page (but somehow different from your overrides) - why not display old object's edit page but under Save button's action have something like
Account newAcc = originalAccount.clone(false,true);
try{
insert newAcc;
return '/' + newAcc.Id;
} catch(Exception e){
ApexPages.addMessages(e);
}
return null;
You can use the $Action global variable right in the VF page. Something like the following
<apex:CommandButton action="{!URLFOR($Action.Your_Object__c.Clone)}" value="Clone"/>
Here's the API for global variables http://www.salesforce.com/us/developer/docs/pages/Content/pages_variables_global.htm
Solution which I am following: on click of clone button I am appending an extra attribute, say clone=1
to the url.
testPage.getParameters().put('clone','1');
Now I am checking if the user has ever hit the clone button before hitting the save button.
Cloneyes = ApexPages.currentPage().getParameters().get('clone');
if(Cloneyes !=null){
save the record as a new record
} else {
modify the old record
}
-
I know this is an old thread, but I wanted to add a shortcut - in my constructor, when I instantiate the object I check if this is a clone (similar to if(Cloneyes != null) and then immediately clone. This way the save method is much more straightforward.– hgolovNov 4, 2014 at 7:38