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
ashish_singhashish_singh 

Does salesforce supports cloning of Sobject using Clone method

below is the code that i have tried to do the cloning.

 

Account acc=new  Account(Name='ACC');
insert acc;
Opportunity CloneOpp= new Opportunity(Name='CloneOpp',AccountId=acc.id,Amount=222,StageName='xyz',CloseDate=System.today(),h1newContractEndDate__c=System.today());
 insert CloneOpp;

 Opportunity colned=CloneOpp.clone(true,false);
      
        
  system.debug('*Id=*'+colned.id);
  system.debug('source id='+CloneOpp.id);
 system.debug('*name*'+colned.Name);
  system.debug('opp='+CloneOpp.Name);


     colned.Name='TestNew';

   system.debug('*name after update*'+colned.Name);
   system.debug('Source opp Name='+CloneOpp.Name);//this should be print TestNew but it prints CloneOpp

 

apex guide says-" If "opt_IsDeepClone" argument set to false, the method creates a reference to
the original sObject. Consequently, if you make changes to a field on the cloned sObject, the original
sObject is also affected".

 

 

please guide if any one has answer of this ..

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

So, you're thinking of the "reference" type as being a true "same memory location" reference for *the SOBJECT* being cloned.  (e.g. what you'd see if you tested using "===" instead of "==".)

 

However, when you clone you *always* get a new SObject.  The deep vs. shallow question doesn't refer to the SObject itself, it refers to data structures inside the Sobject that refer to other sobjects.

 

For example:

 

 

Account acc=new  Account(Name='ACC');
insert acc;

// Create the Opp.
Opportunity opp = new Opportunity(Name='CloneOpp',AccountId=acc.id,Amount=222,StageName='xyz',CloseDate=System.today());
insert opp;

// Load the original back, this time also getting a referenced Account object.
Opportunity original = [select Name, Account.Name from Opportunity where id=:opp.id];

// Clone both ways.
Opportunity deep = original.Clone(true,true);
Opportunity shallow = original.Clone(true,false);

// All the Account Names will be the same.
system.debug('original.Account.Name: ' + original.Account.Name);
system.debug('deep.Account.Name: ' + deep.Account.Name);
system.debug('shallow.Account.Name: ' + shallow.Account.Name);

original.Account.Name = 'Peanuts';

// Only the Shallow one will change.
system.debug('original.Account.Name: ' + original.Account.Name);
system.debug('deep.Account.Name: ' + deep.Account.Name);
system.debug('shallow.Account.Name: ' + shallow.Account.Name);

 

Best, Steve.

 

All Answers

SteveBowerSteveBower

So, you're thinking of the "reference" type as being a true "same memory location" reference for *the SOBJECT* being cloned.  (e.g. what you'd see if you tested using "===" instead of "==".)

 

However, when you clone you *always* get a new SObject.  The deep vs. shallow question doesn't refer to the SObject itself, it refers to data structures inside the Sobject that refer to other sobjects.

 

For example:

 

 

Account acc=new  Account(Name='ACC');
insert acc;

// Create the Opp.
Opportunity opp = new Opportunity(Name='CloneOpp',AccountId=acc.id,Amount=222,StageName='xyz',CloseDate=System.today());
insert opp;

// Load the original back, this time also getting a referenced Account object.
Opportunity original = [select Name, Account.Name from Opportunity where id=:opp.id];

// Clone both ways.
Opportunity deep = original.Clone(true,true);
Opportunity shallow = original.Clone(true,false);

// All the Account Names will be the same.
system.debug('original.Account.Name: ' + original.Account.Name);
system.debug('deep.Account.Name: ' + deep.Account.Name);
system.debug('shallow.Account.Name: ' + shallow.Account.Name);

original.Account.Name = 'Peanuts';

// Only the Shallow one will change.
system.debug('original.Account.Name: ' + original.Account.Name);
system.debug('deep.Account.Name: ' + deep.Account.Name);
system.debug('shallow.Account.Name: ' + shallow.Account.Name);

 

Best, Steve.

 

This was selected as the best answer
ashish_singhashish_singh

I REALLY APPRICIATE YOUR HELP...

 

manjeeramanjeera

Through Code we are creating copy of sobject. After creation of copy , where to find that clone object? I am not able to find that clone object in custom objects list.

 

   Please let me know

 

Thanks,

manju

S_2013S_2013

Hi,

 

I was just having same question when i got to see your post. I have a doubt on this ... can you plz help me clarify?

 

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;

When I go into system and see the Ids of c, cdeep, cshallow and their accounts, I have 3 diff. ids for c, cdeep and cshallow, but all referring to one single Account! so i still fail to understand what is the difference between using deep clone and shallow clone...

 

ISVforce PartnerISVforce Partner

Although this is an old post and already answered, I would like to mention about "iClone" app. It may help someone looking for similar needs, but do not have developer skills or the time to develop. 

 

Here is the link  https://appexchange.salesforce.com/listingDetail?listingId=a0N3000000B39tWEAR

 

You can clone any object with any related list. You can choose which fields to clone and provide default values as well.  

 

sebastiangnagnarellasebastiangnagnarella

Programatically, sobject.clone() clones objects, however, taking it to Salesforce jargon, it is actually clonning records.

sebastiangnagnarellasebastiangnagnarella

Something weird I saw happening with the deep clone , that actually makes sense, is that the collection of clonned children object still point to the original master.

 

In this example I iterate through the clone children just to re assign them to the new Parent :

 

        Trip_Proposal__c origtp = [SELECT Member_Name__c, Proposal_Source__c, Proactive_Reason__c, Description__c, Check_In_Date__c, Check_Out_Date__c,
                                   Flexible__c, Flexible_Note__c, Trip_Purpose__c, Other_Trip_Purpose__c, No_of_Companions__c, Trip_Notes__c,
                                   (SELECT Trip_Proposal__c, Property__c, Avg_Nightly_Rate__c, Additional_Fees__c, Check_In_Date__c, Check_Out_Date__c FROM  Trip_Options__r)
                                   FROM Trip_Proposal__c WHERE Id = :tripPorposalId];
                               
		Trip_Proposal__c tp = origtp.clone(false, true);
        tp.Status__c = 'Open';
      	
        insert tp;
        
        
        for(Trip_Proposal_Options__c tpo : tp.Trip_Options__r){
        	tpo.Id = null;
            tpo.Trip_Proposal__c = tp.Id;
            insert tpo;
        }