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
RIteshMRIteshM 

preserving Id in sObject.clone() function

i have to clone an object means copy all fields in another object. all the fields will remain same except one which is not writable .first thing is in sObject.clone() function how is it possible to have two objects having same Id.Please explain a little workflow and input and output of clone function

 

Avidev9Avidev9

So here it is

  • The method clones it at a code level. it something like you can have to instance of same record.  Extendting the example its similar to having two variables with the same value.
  • The two instance will share the same Id but you wont be able to insert the record. Will you be ? Remember to insert a record the Id needs to be blank

Furher expanantion of two objects having same Id.

 

You can do something like this

 

Account a1 = [SELECT Id FROM Account WHERE Id='001ad58526asdsd'];

Account a2 = [SELECT Id FROM Account WHERE Id='001ad58526asdsd'];

 

Both a1 and a2 will same Id, but this doesnt mean there are two records in DB with same Id.

 

Similarly you can do something like to clone and preserve the Id

 

Account a1 = [SELECT Id FROM Account WHERE Id='001ad58526asdsd'];

Account a2 = a1.clone(true,false,false,false);

 

* a1 and a2 are two differenct instances

 

 

 

 

RIteshMRIteshM

ok i got your point of two instances can u please tell me if i have to make all field same of a record as given record except id which is autogenerated can we generate such record and then insert it in database please guideline how to generate copy of a record and then inserting it in database.

Avidev9Avidev9

Here is an example

 

Account a1 = [SELECT Id FROM Account WHERE Id='001ad58526asdsd'];

Account a2 = a1.clone(false,false,false,false); //all the writtable fields are queried

//lets say we want to change the name keeping everything same

a2.Name = a2.Name +'Cloned';

insert a2;

RIteshMRIteshM

there is a relationship field in the CustomObject__c let us say field__c which is not writable if we have to duplicate an object delete original object and put this copied object in database in this scenario if there are two relationship fields then we want the second relationship field copied for that i think we have to put 

 

CustomObject__c  cc =[SELECT Id FROM CustomObject__c WHERE Id='001ad58526asdsd'];

CustomObject__c cc1= cc.clone(false,true,false,false);

cc1.field__c = 'Customize value';

delete cc;insert cc1;

 

and i want  to confirm  1. Not writable field can be assigned before insertion and after that they can't be modified is this assumption is true or false ??

                                         2.in clone we have to provide true for copying value of relationship field in second argument of clone method

please correct if i am wrong in these two assumptions

Avidev9Avidev9
You are correct