How to Clone a Record In Apex? : ashutosh pandey

How to Clone a Record In Apex?
by: ashutosh pandey
blow post content copied from  Forcetalks
click here to view original post


What is Clone Record in Apex?

The clone() method in Salesforce Apex makes a duplicate of the sObject record. There are four Boolean-type optional parameters for this procedure. 

Clone Method

clone(preserveId, isDeepClone, preserveReadonlyTimestamps, preserveAutonumber)

What are the Parameters to Clone Records?

  1. opt_preserve_id : Opt preserve id controls whether the duplicate object's ID is cleared or preserved from the original. The ID is replicated to the duplicate if we set it to true. The ID is cleared when the default value of false is used.
  2. opt_IsDeepClone: Determines if the method generates a full duplicate of the sObject field or only a reference using the opt IsDeepClone parameter. The method makes a complete duplicate of the sObject if true is set. Every field on the sObject, including the relationship fields, is copied in memory. As a result, the original sObject is unaffected if you alter a field on the cloned sObject. The method performs a shallow copy of the sObject fields if false is set. The original sObjects are referenced in every copied relationship field. As a result, any changes you make to a relationship field on the cloned sObject also affect its corresponding field on the original sObject, and vice versa. True is the default.
  3. opt_preserve_readonly_timestamps: Determines whether or not the duplicate's read-only timestamp data are cleared. The read-only fields CreatedById, CreatedDate, LastModifiedById, and LastModifiedDate are copied to the duplicate if the value of the field is set to true. The values are cleared when the default value is false.
  4. opt_preserve_autonumber: Determines whether the duplicate object's auto number fields are cleared or maintained from the original. Auto number fields are copied to the cloned object if the value is set to true. The auto number fields are cleared when the default setting of false is used.

dont miss out iconCheck out another amazing blog by Ashutosh here: What is the Flosum Tool in Salesforce in 2023?

Code

In this class(cloneRecordCtrl), we use Salesforce to Clone Record by Apex. Therefore, we make a new record. 

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

The post How to Clone a Record In Apex? appeared first on Forcetalks.


January 14, 2023 at 12:21AM
Click here for more details...

=============================
The original post is available in Forcetalks by ashutosh pandey
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Salesforce