+ Start a Discussion
JieMeJieMe 

object record clone

sobject method ,clone(),has four parameters,the first " opt_preserve_id" argument
determines whether the ID of the originalobject is preserved or cleared in the duplicate.
If set to true, the ID is copied to theduplicate. The default is false, that is, the IDis cleared.apex api document .
when i set ,clone(true);it does not work .page is showed:account id can not be inserted.


 Savepoint sp = Database.setSavepoint();
         account newacc;
 
         try {
 
              //copy the purchase order - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             acc = [select Id, Name from account where id = :acc.id];
             newacc = acc.clone(true);
             insert newacc;
 
             // set the id of the new acc created for testing
               newRecordId = newacc.id;
 
             // copy over the line cons - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             List<Contact> cons = new List<Contact>();
             for (Contact con : [Select c.Id,c.Name From Contact c where accountid = :acc.id]) {
                  Contact newcon = con.clone(true);
                  newcon.accountid = newacc.id;
                  cons.add(newcon);
             }
             insert cons;
 
         } catch (Exception e){
             // roll everything back in case of error
            Database.rollback(sp);
            ApexPages.addMessages(e);
            return null;
         }

 

hi guys,did u meet the case?

Thanks!


Best Answer chosen by Admin (Salesforce Developers) 
MoUsmanMoUsman

Hi JieMe,

Here we need to pass two argument in clone method first is false and second in true, then your problem solved to explore colne method click here

 

 Savepoint sp = Database.setSavepoint();
account newacc;
Id newRecordId;
 account acc;
         try { 
              //copy the purchase order - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             acc = [select Id, Name from account limit 1];
             newacc = acc.clone(false,true);
             insert newacc;
 
             // set the id of the new acc created for testing
               newRecordId = newacc.id;
 
             // copy over the line cons - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             List<Contact> cons = new List<Contact>();
             for (Contact con : [Select c.Id,c.Name From Contact c where accountid = :newacc.id]) {
                  Contact newcon = con.clone(false,true);
                  newcon.accountid = newacc.id;
                  cons.add(newcon);
             }
             insert cons;
 
         } catch (Exception e){
             // roll everything back in case of error
            Database.rollback(sp);
            // ApexPages.addMessages(e);
            return null;
         }
 
Note: This code is well tested from Eclipe Anonymous.
I hope it will help you :)
--
Regards
Usman

 

All Answers

MoUsmanMoUsman

Hi JieMe,

Here we need to pass two argument in clone method first is false and second in true, then your problem solved to explore colne method click here

 

 Savepoint sp = Database.setSavepoint();
account newacc;
Id newRecordId;
 account acc;
         try { 
              //copy the purchase order - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             acc = [select Id, Name from account limit 1];
             newacc = acc.clone(false,true);
             insert newacc;
 
             // set the id of the new acc created for testing
               newRecordId = newacc.id;
 
             // copy over the line cons - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             List<Contact> cons = new List<Contact>();
             for (Contact con : [Select c.Id,c.Name From Contact c where accountid = :newacc.id]) {
                  Contact newcon = con.clone(false,true);
                  newcon.accountid = newacc.id;
                  cons.add(newcon);
             }
             insert cons;
 
         } catch (Exception e){
             // roll everything back in case of error
            Database.rollback(sp);
            // ApexPages.addMessages(e);
            return null;
         }
 
Note: This code is well tested from Eclipe Anonymous.
I hope it will help you :)
--
Regards
Usman

 

This was selected as the best answer
JieMeJieMe

thank for your response.

according your advice,that can work.i need the id of original object is also copied.

so,how to look at the original id on new insert account?

JieMeJieMe

hi ,MoUsman

 

i got it.

 

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.

 

Thanks!

 

 

MoUsmanMoUsman

HI JieMe,

 

I was not aware with your complete requirment, I thought you want to create duplicate record but salesforce does not allow you to cretae new record with same Id as you know every record have a unique Id,so that's way I replied to you.

 

---

Thanks

Usman