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
satishch_sfdcsatishch_sfdc 

Doubts in clone method(sObject)

Hi,

This is satish, When i go through clone(sObject) method, I got few doubts in the signature of the method.

Method 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.



Here is my first question:
                If I make first parameter true, the ID will be copied to duplicate record.But how the two records will have same ID? For what purpose this Parameter we have to use?


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.


Here is my second question:
 
                I made this third parameter true, But the read-only timestamp fields are not copied to the child record. I tried it for sample records. The copied record showing the current logged in username and current datetime. Why this is not working as it is mentioned above?

 
Please clarify these two, Thanks in advance.
 
Thanks,
Satish
 
ShashankShashank (Salesforce Developers) 
The Clone method makes a copy of the record to be used within the apex class or trigger. It will not insert the record in the database. Hence, you have the option to copy the Id and system timestamp fields in a cloned copy of the record. But when you insert these records, the Id or the timestamp fields will not be editable.