You need to sign in to do that
Don't have an account?
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!
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
All Answers
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
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?
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!
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