Salesforce | Find out how to Clone a Document In Apex?


What’s Clone Document in Apex?

The clone() technique in Salesforce Apex makes a replica of the sObject document. There are 4 Boolean-type non-obligatory parameters for this process. 

Clone Technique

clone(preserveId, isDeepClone, preserveReadonlyTimestamps, preserveAutonumber)

What are the Parameters to Clone Data?

  1. opt_preserve_id : Choose protect id controls whether or not the duplicate object’s ID is cleared or preserved from the unique. The ID is replicated to the duplicate if we set it to true. The ID is cleared when the default worth of false is used.
  2. opt_IsDeepClone: Determines if the strategy generates a full duplicate of the sObject discipline or solely a reference utilizing the decide IsDeepClone parameter. The tactic makes a whole duplicate of the sObject if true is ready. Each discipline on the sObject, together with the connection fields, is copied in reminiscence. In consequence, the unique sObject is unaffected in the event you alter a discipline on the cloned sObject. The tactic performs a shallow copy of the sObject fields if false is ready. The unique sObjects are referenced in each copied relationship discipline. In consequence, any modifications you make to a relationship discipline on the cloned sObject additionally have an effect on its corresponding discipline on the unique sObject, and vice versa. True is the default.
  3. opt_preserve_readonly_timestamps: Determines whether or not or not the duplicate’s read-only timestamp knowledge are cleared. The read-only fields CreatedById, CreatedDate, LastModifiedById, and LastModifiedDate are copied to the duplicate if the worth of the sector is ready to true. The values are cleared when the default worth is fake.
  4. opt_preserve_autonumber: Determines whether or not the duplicate object’s auto quantity fields are cleared or maintained from the unique. Auto quantity fields are copied to the cloned object if the worth is ready to true. The auto quantity fields are cleared when the default setting of false is used.

dont miss out iconTry one other wonderful weblog by Ashutosh right here: What is the Flosum Tool in Salesforce in 2023?

Code

On this class(cloneRecordCtrl), we use Salesforce to Clone Document by Apex. Due to this fact, we make a brand new document. 

public class cloneRecordCtrl
{
    public static void cloneRecord()
    {
        // retrive contact document for clone
        Contact con = [SELECT FirstName, Email, LastName FROM Contact LIMIT 1];
        // clone document
        Contact conCopy = con.clone(false, false, false, false);
        insert conCopy;
    }
}





Source link

Thanks for Reading

Enjoyed this post? Share it with your networks.

Leave a Feedback!