Salesforce sObject class has a method called clone() which creates a copy of the sObject record. This method has four Boolean type optional parameters.
preserveId: Determines whether the ID of the original object is kept 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.
isDeepClone: 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.
preserveReadonlyTimestamps: 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.
preserveAutonumber: 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.
Example
Below are the few of the cloning methods:
isClone() - Returns true if an entity is cloned from something, even if the entity hasn’t been saved.
Example
getCloneSourceId() - Method gives the salesforce id of the source record from where the new record is cloned.
Example
NOTE: getCloneSourceId() Always return null in salesforce lightning. This is already reported as BUG in salesforce.
clone(Boolean preserveId, Boolean isDeepClone, Boolean preserveReadonlyTimestamps, Boolean preserveAutonumber)
preserveId: Determines whether the ID of the original object is kept 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.
isDeepClone: 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.
preserveReadonlyTimestamps: 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.
preserveAutonumber: 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.
Example
Account objAcc = new Account(Name = 'Test Acc', Industry = 'Banking', Phone = '1234567890');
insert objAcc;
//Clone the record
Account accClone = objAcc.clone(false, false, false, false);
insert accClone;
Below are the few of the cloning methods:
isClone() - Returns true if an entity is cloned from something, even if the entity hasn’t been saved.
Example
trigger clonetest on sObject (before insert) {
for (sObject obj : Trigger.new) {
if (obj.isClone()) {
System.debug( 'isClone: ====> '+ obj.isClone());
}
}
}
getCloneSourceId() - Method gives the salesforce id of the source record from where the new record is cloned.
Example
trigger clonetest on sObject (before insert) {
for (sObject obj : Trigger.new) {
if (obj.isClone()) {
System.debug( ' isClone: ====> '+ obj.getCloneSourceId());
}
}
}
NOTE: getCloneSourceId() Always return null in salesforce lightning. This is already reported as BUG in salesforce.
No comments:
Post a Comment