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
Sachin10Sachin10 

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>
  <apex:column headerValue="Rating">
  <apex:inputField value="{!acc.rating}"/>
  </apex:column>  
  </apex:pageBlockTable>
  <apex:pageblockButtons >
   <apex:commandButton value="FetchAccounts" action="{!fetchAccounts}" reRender="pgblkId"/>
  <apex:commandButton value="Save" action="{!save}" reRender=""/>
  </apex:pageblockButtons>
  </apex:pageblock>
  </apex:form>
</apex:page>


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.add(acc);
        }
       // 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.
Sachin10Sachin10
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.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm
Hope this helps.
 
Sachin10Sachin10
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>.
Sachin10Sachin10
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>.