+ Start a Discussion

Copy one list to another without being impacted by the changes done in other list

Hi All,
I would like to copy one list to another without the changes done in one list impacting the other.
Basically I will have 2 lists. One holds the initial values and the other list which will be changed on visualforce page.
At the end I want to compare the changes between the lists.

I tried the following ways to copy the list (addAll,Clone,and assignment,for loop). In all cases the if I change one list the other list is also changing its values.
Refer the below code snippet. When the accountList changes the initialAccountList also changes.

Visualforce Page:
<apex:page controller="ListCopyController">
  <apex:form >
  <apex:pageblock title="Accounts List Copy" id="pgblkId">
  <apex:pageBlockTable value="{!accountList}" var="acc">
  <apex:column headerValue="Name">
  <apex:inputField value="{!acc.name}"/>
  <apex:column headerValue="Rating">
  <apex:inputField value="{!acc.rating}"/>
  <apex:pageblockButtons >
   <apex:commandButton value="FetchAccounts" action="{!fetchAccounts}" reRender="pgblkId"/>
  <apex:commandButton value="Save" action="{!save}" reRender=""/>

Apex Controller:
public class ListCopyController {

    public list<account> accountList{get;set;}
    public list<account> initialAccountList{get;set;}
    public ListCopyController(){
        accountList = new list<account>();
        initialAccountList = new list<account>();        
    public void fetchAccounts(){
        accountList = [select id,name,rating from account limit 5];

        for(account acc: accountList){
       // initialAccountList = accountList;
       // initialAccountList.addAll(accountList);
        //initialAccountList = accountList.clone();    
    public void save(){
        system.debug('@@ initialAccountList'+ initialAccountList);
        system.debug('@@ accountList'+ accountList);
        update accountList;


Looks simple but couldn't find anything relevant while searching.
Any pointers/suggestions are highly appreciated.
Many Thanks!
sandip bijlwansandip bijlwan
oOne way to add is iterate over accountList and add it to the another list one by one. And at the time of update be sure that no duplicate account present in the list.
Thanks for your reply.
If you see the code snippet that I posted, I was doing the same. But it didn't work.
Can you please check & let me know if I am missing something.
Dushyant SonwarDushyant Sonwar
Hi Sachin,

You can try with clone method of sobject class. There is isDeepClone parameter that can help you.
Hope this helps.
Thanks for your reply.
It works in this scenario, but the actual requirement is to copy a list<wrapperClass> to another list<wrapperClass>.
I think this will be limited to sObject alone.Let me know if we have something for that.

Dushyant SonwarDushyant Sonwar
NO , you are wrong .It will also work for copying list<wrapperClass> to another list<wrapperClass>.
Can you please share a code snippet on how to clone a list<wrapperClass> to another list<wrapperClass>?
I tried, but the deepClone method is supported on sObject and not on List<wrapperClass>.