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

How to replicate Standard Clone Behavior for VF Page

I have a custom object where the Detail view, edit, and new pages have all been overriden with visualforce pages. I need to be able to create/override a clone button that works like the standard clone button. So far I have only been able to override the clone button so that when it is clicked the record is created (new record inserted) then the page redirects to the edit page of the new record.


How do I make the clone work like the standard clone? In the standard clone when the clone button is clicked the user is redirected to an edit page where the record id in the url is the record you are trying to clone. After the user clicks save is when the new record gets "cloned"


Would I have to override the standard save behavior? Just for the clone page?


I can post the code I have completed so far if needed.


Here's some sample code you may find useful...


The VF Page:

<apex:page standardController="Target__c" showHeader="true" sidebar="true" tabStyle="Target__c" extensions="targetCloneExt"> <style> .nField { width: 380px; height:80px; } </style> <apex:sectionHeader title="Target Edit" subtitle="Clone Target" /> <apex:form id="myForm"> <apex:pageBlock title="Target Edit" mode="edit" id="pgB"> <apex:pageMessages id="tError" ></apex:pageMessages> <apex:pageBlockButtons > <apex:commandButton value="Save" action="{!save}" /> <apex:commandButton value="Cancel" action="{!cancel}"/> </apex:pageBlockButtons> <apex:pageBlockSection title="Information" columns="2" id="pgBSec1"> <apex:inputField id="tValid" value="{!Target__c.Valid__c}" /> <apex:inputField id="tContract" value="{!Target__c.Contract__c}" required="true"/> <apex:inputField id="tIDate" value="{!Target__c.Initial_Date__c}" required="true"/> <apex:inputField id="tPGroup" value="{!Target__c.Product_Group__c}" /> <apex:inputField id="tFDate" value="{!Target__c.Final_Date__c}" required="true"/> <apex:inputField id="tProd" value="{!Target__c.Product__c}" /> <apex:outputText value=""/> <apex:inputField id="tNotes" value="{!Target__c.Notes__c}" styleClass="nField"/> </apex:pageBlockSection> <apex:pageBlockSection title="System Information" columns="2" id="pgBSec2" rendered="false"> <apex:inputField id="tName" value="{!Target__c.Name}" /> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>



The Controller:



public class targetCloneExt { Contract contract; //Container for Target being cloned: Target__c nTarget; public targetCloneExt(ApexPages.StandardController stdController) { nTarget = (Target__c)stdController.getRecord(); } public Contract getContract() { contract = [SELECT Id, AccountId, ContractNumber, Account.Name, StartDate, EndDate FROM Contract WHERE id = :nTarget.contract__c]; return contract; } public PageReference save(){ //Container for new (cloned) target //The 'false' parameter discards the old id

// so we can insert the cloned target Target__c cTarget = nTarget.clone(false); try { insert cTarget; } catch ( DmlException exc) { ApexPages.addMessages(exc); return null; } //Instead of returning to the new 'cloned' target, return to the parent contract return new PageReference('/' + cTarget.Contract__c); } }


Message Edited by CaptainObvious on 02-16-2010 05:09 PM

i'm not a java script coder, nor an apex coder, but i'm trying...  i don't really understand classes, etc...


i'm trying to do a custom clone button for opportunity.  i've been successful using code i found in the community, but the record is immediately saved... i, like MSVRad, need to replicate the standard clone behavior (as i have many validation rules that will potential fail on an insert that the user needs to see)...


the code i'm using is a button that is defined to execute java script and is pretty basic... what you have here is a tad above my head, but i need the save vs. cancel functionality.... here's the code i'm using (i'm sure it will look familiar)...


any suggestions/help would be greatly appreciated.  thanks much.





var result = sforce.connection.query("Select o.StageName, o.Amount, o.AccountId, o.Quantity__c, o.Adhesive_Seal__c, o.Price_Unit__c, o.Priced_Per__c, o.Adjustable_Die__c, o.Artwork_Furnished_Via__c, o.Back_PMS_1__c, o.Back_PMS_2__c, o.Back_PMS_3__c, o.Back_PMS_4__c, o.Black_Back__c, o.Black_Front__c, o.Box__c, o.Carton__c, o.Carton_Quantity__c, o.Cost_M__c, o.Cut_Sheets_To__c, o.DPE_Inventory__c, o.Envelope_Die__c, o.Envelope_Die_Sublist__c, o.Envelope_Height_inches__c, o.Envelope_Style__c, o.Envelope_Width_inches__c, o.Envelopes_Furnished__c, o.RecordTypeId, o.Film__c, o.Film_Quantity__c, o.Flap_Style__c, o.Flexo__c, o.Outside_PMS_1__c, o.Outside_PMS_2__c, o.Form__c, o.Front_PMS_1__c, o.Front_PMS_2__c, o.Front_PMS_3__c, o.Front_PMS_4__c, o.Grain__c, o.Internal_Notes_Materials_Etc__c, o.No_of_Bleeds_Back__c, o.No_of_Bleeds_Front__c, o.No_of_Colors_Back__c, o.No_of_Colors_Front__c, o.No_of_Negatives__c, o.Custom_Tint_Description__c, o.Window_1_Orientation__c, o.Window_Die__c, o.Window_Die_Sublist__c, o.Window_1_Distance_from_Bottom__c, o.Window_1_Distance_from_Left__c, o.Window_1_Height__c, o.Other_Window_Die_Specify__c, o.Window_1_Width__c, o.Window_2_Orientation__c, o.Window_2_Die__c, o.Window_2_Die_Sublist__c, o.Window_2_Distance_from_Bottom__c, o.Window_2_Distance_from_Left__c, o.Window_2_Height__c, o.Window_2_Other_Die_Specify__c, o.Window_2_Width__c, o.Previous_Order__c, o.Previous_Order_Date__c, o.Exact_Repeat__c From Opportunity o WHERE o.Id = '{!Opportunity.Id}'");

var newOpp = result.getArray("records");

var user = sforce.connection.getUserInfo();

// Reset the Opp Id and reset fields to default values

newOpp[0].Id = '';

newOpp[0].Name = "{!Opportunity.Name}";
newOpp[0].StageName = "Closed Won";
newOpp[0].CloseDate = new Date();
newOpp[0].Cloned_From__c = "{!Opportunity.SF_Order_Number__c}";
newOpp[0].Desert_Employee_Accepting_Order__c = sforce.connection.getUserInfo().userId;
newOpp[0].Jet_0_Due_Date__c = new Date(2099, 0 , 1);

var saveResult = sforce.connection.create(newOpp);

if (saveResult[0].getBoolean("success")) {
newOpp[0].id = saveResult[0].id;
else {
alert("*** Failed to create clone: " + saveResult[0]);

// Refresh the page to display the new oppportunity
window.location = newOpp[0].id;
catch (err) {
alert (err.description );