+ Start a Discussion
S_2013S_2013 

Deep and shallow clone?

Please help me i am thoroughly confused...

 

Account a = new Account (Name = 'San Clone');
insert a;
Contact c = new Contact (Lastname = 'Chatterjee', AccountId=a.Id);
insert c;
Contact cdeep = c.clone(false, true);
Contact cshallow = c.clone(false, false);
insert cdeep; insert cshallow;
The code here is creating 3 seperate contact objects but all refering to one single account... so what does this word in Apex documentation mean? "The optional opt_IsDeepClone argument determines whether the method creates a full copy of the sObject field, or just a reference:

  • If set totrue, 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."
Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

"Deep Clone" isn't relevant to your code. It's referring specifically to relationships on an SObject, not individual fields (an ID field is just an individual field, while a relationship is an entire SObject). Some code is order:

 

Account a = new Account(Name='test');
Contact cOrg = new Contact(LastName='test');
cOrg.Account = a;
Contact cShal = cOrg.clone(false,false), cDeep = cOrg.clone(false,true);
System.assertEquals('test',a.name);
System.assertEquals('test',cOrg.account.name);
System.assertEquals('test',cShal.account.name);
System.assertEquals('test',cDeep.account.name);
cDeep.account.name = 'test 2';
System.assertEquals('test',a.name);
System.assertEquals('test',cOrg.account.name);
System.assertEquals('test',cShal.account.name);
System.assertEquals('test 2',cDeep.account.name);
cShal.account.name = 'test 3';
System.assertEquals('test 3',a.name);
System.assertEquals('test 3',cOrg.account.name);
System.assertEquals('test 3',cShal.account.name);
System.assertEquals('test 2',cDeep.account.name);

An SObject is an object. An object is a specific point in memory, stored at an "address." What the "deep clone" parameter does is tells Apex Code to either copy the memory address (less memory, but changes to any reference changes all of them), or copy the entire object (decouples the reference so they can be used independently, uses more memory).

 

Your code didn't use an sobject relationship, so there was no extra processing (e.g. true and false would have exhibited the same behavior in your code).

All Answers

sfdcfoxsfdcfox

"Deep Clone" isn't relevant to your code. It's referring specifically to relationships on an SObject, not individual fields (an ID field is just an individual field, while a relationship is an entire SObject). Some code is order:

 

Account a = new Account(Name='test');
Contact cOrg = new Contact(LastName='test');
cOrg.Account = a;
Contact cShal = cOrg.clone(false,false), cDeep = cOrg.clone(false,true);
System.assertEquals('test',a.name);
System.assertEquals('test',cOrg.account.name);
System.assertEquals('test',cShal.account.name);
System.assertEquals('test',cDeep.account.name);
cDeep.account.name = 'test 2';
System.assertEquals('test',a.name);
System.assertEquals('test',cOrg.account.name);
System.assertEquals('test',cShal.account.name);
System.assertEquals('test 2',cDeep.account.name);
cShal.account.name = 'test 3';
System.assertEquals('test 3',a.name);
System.assertEquals('test 3',cOrg.account.name);
System.assertEquals('test 3',cShal.account.name);
System.assertEquals('test 2',cDeep.account.name);

An SObject is an object. An object is a specific point in memory, stored at an "address." What the "deep clone" parameter does is tells Apex Code to either copy the memory address (less memory, but changes to any reference changes all of them), or copy the entire object (decouples the reference so they can be used independently, uses more memory).

 

Your code didn't use an sobject relationship, so there was no extra processing (e.g. true and false would have exhibited the same behavior in your code).

This was selected as the best answer
S_2013S_2013

Thanks a lot sdfcfox!