You need to sign in to do that
Don't have an account?

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!
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!
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.
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.
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.
I tried, but the deepClone method is supported on sObject and not on List<wrapperClass>.