2

I have a VF page that is the 'edit' page for a certain object. I have a button 'Clone'. Upon clicking this button, I wish to be redirected to a new edit page with all the previous objects fields still intact (except for a few that should be cleared and set to default). Note that this new object should not be inserted until I click 'Save'. The Id of the object should be different compared to the previous object, so that on Save, the previous object is not overwritten.

I am thinking I will split this up into two parts, an apex method, and a javascript method for resetting the fields:

<apex:commandButton action="{!saveAsTemplate}" onclick="initTemplate();" value="Clone" />

The javascript portion is easy enough, but I'm having trouble of figuring out what to do on the apex side, besides for setting the id of the record to null.

1 Answer 1

3

You could create a record with just the fields set that you want to copy over and then clone that sObject record, similar to this example here.

//copy the purchase order - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
po = [select Id, Name, Ship_To__c, PO_Number__c, Supplier__c, Supplier_Contact__c, Date_Needed__c, Status__c, Type_of_Purchase__c, Terms__c, Shipping__c, Discount__c from Purchase_Order__c where id = :po.id];
             newPO = po.clone(false);
             insert newPO;

More about the clone method can be found here:

clone(Boolean, Boolean, Boolean, Boolean)

Creates a copy of the sObject record.

Signature
public sObject clone(Boolean opt_preserve_id, Boolean opt_IsDeepClone, Boolean opt_preserve_readonly_timestamps, Boolean opt_preserve_autonumber)

Parameters

opt_preserve_id
Type: Boolean
(Optional) Determines whether the ID of the original object is preserved or cleared in the duplicate. If set to true, the ID is copied to the duplicate. The default is false, that is, the ID is cleared.

opt_IsDeepClone
Type: Boolean
(Optional) Determines whether the method creates a full copy of the sObject field, or just a reference:

  • If set to true, the method creates a full copy of the sObject. All fields on the sObject are duplicated in memory, including relationship fields. Consequently, if you make changes to a field on the cloned sObject, the original sObject is not affected.
  • If set to false, the method performs a shallow copy of the sObject fields. All copied relationship fields reference the original sObjects. Consequently, if you make changes to a relationship field on the cloned sObject, the corresponding field on the original sObject is also affected, and vice-versa. The default is false.

opt_preserve_readonly_timestamps
Type: Boolean
(Optional) Determines whether the read-only timestamp fields are preserved or cleared in the duplicate. If set to true, the read-only fields CreatedById, CreatedDate, LastModifiedById, and LastModifiedDate are copied to the duplicate. The default is false, that is, the values are cleared.

opt_preserve_autonumber
Type: Boolean
(Optional) Determines whether auto number fields of the original object are preserved or cleared in the duplicate. If set to true, auto number fields are copied to the cloned object. The default is false, that is, auto number fields are cleared.
Return Value
Type: sObject (of same type)

2
  • I'll definitely look into this. Problem is, I don't want to insert the record, reason being, some of the fields I want to clear are actually required fields, meaning I couldn't insert the record even if I wanted. I simply want a new edit page for the record to appear, and then have the choice to save/insert or not.
    – logeyg
    Dec 5, 2014 at 2:08
  • @logan_gabriel you should be able to skip the insert part and just display the clone result on the edit page.
    – Jenny B
    Dec 5, 2014 at 2:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .