Can I deep clone a record but set the ID

apexcloneloop

I have two account records. I want to move all of the data from account #1 into account #2. Is it possible to do a deep clone and set the ID? Or would I have to loop through account #1 and pull the data from each field and push it into account #2?

Here is some code that might help clarify what I am looking to do:

    //OLD Account record
    Account accountMergeOLD = [SELECT * FROM Account WHERE Id = '999999999999999'];
    //NEW Account record
    Account accountMergeNEW = [SELECT * FROM Account WHERE Id = '333333333333333'];

    //we want to merge them 
    accountMergeNEW = accountMergeOLD.clone(false, true, false, true);

Note: I know that SOQL doesn't support the * selector, I just put it in to display the need for all fields

Best Answer

Since you want to move the data between existing accounts from Account1 to Account2, use clone() on Account1 to get all the data and then use merge on Account2 and cloned account.

clone() : https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm#apex_System_SObject_clone

merge : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_merge.htm

Related Topic