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
SirTravSirTrav 

Copy wrapper class

I have a wrapper class that has a number of objects in it.  I then have a list of that class that is displayed on a visualforce page.  what I want to do is take the initial list and copy it and put it in a second list.  Then I want to be able to edit the first list without the second list being changed.  Here is what the code looks like the one below but I have shortened the code by removing some standard stuff and puting ...... in its place.  what I am trying to do is leave the original list in place on the visualforce page to be edited and added back to the second list with slightly different answers.  The problem is that when the original list is changed it changes the second list. 

public void runAtStart(){
     MwList = new list<MyWrapper>();
     OpportunityLineItem o = new OpportunityLineItem();
     o.opportunityid = .........
     MyWrapper mw = new MyWrapper();
     mw.oli = .......
     MwList.add(mw);   
}

public class MyWrapper{
     public opportunitylineitem oli {get;set;}
     public string s {get;set;}
     public integer i {get;set;}
}

public list<MyWrapper> MwList {get;set;}
public list<MyWrapper> WrapperList2 {get;set;}

public void ToSecondList(){
     if(WrapperList2 == null){
          WrapperList2 = new list<MyWrapper>();
     }
     for(MyWrapper m : MwList){
          MyWrapper w = new MyWrapper();
          w = m.clone();
          WrapperList2.add(w);
     }
}

}


pconpcon
The problem is you'll need to also clone all of the underlying objecs as well.  I would recommend adding a constructor to your MyWrapper class that takes an instance of it's self in and then use that.  For example:

public class MyWrapper {
     public opportunityLineItem oli {
          get;
          set;
     }

     public String s {
          get;
          set;
     }

     public Integer i {
          get;
          set;
     }

     public MyWrapper() {}

     public MyWrapper(MyWrapper wrapper) {
          this.oli = wrapper.oli.clone(true, false, false, false);
          this.s = wrapper.s;
          this.i = wrapper.i;
     }
}

public void toSecondList() {
     if (wrapperList2 == null) {
          wrapperList2 = new List<MyWrapper>();
     }

     for (MyWrapper wrapper: mwList) {
          wrapperList2.add(new MyWrapper(wrapper));
     }
}
NOTE: This code has not been tested and may contain typographical or logical errors.

You may need to adjust the booleans on the .clone(..) method on the oli, but that should be a start.
SirTravSirTrav
That works good for cloning them but I still have the same issue that when mwList is changed it also changes wrapperList2
pconpcon
Then there must be something else in your code that is causing that.  If I run the following in the developer console

public Class MyWrapper {
    public OpportunityLineItem oli;
    public String s;
    public Integer i;
    
    public MyWrapper() {}
    public MyWrapper(MyWrapper wrapper) {
        this.oli = wrapper.oli.clone(true, false, false, false);
        this.s = wrapper.s;
        this.i = wrapper.i;
    }
}

List<MyWrapper> oldList = new List<MyWrapper>();
List<MyWrapper> newList = new List<MyWrapper>();

MyWrapper test1 = new MyWrapper();
test1.s = 'foo1';
test1.oli = new OpportunityLineItem(Description='foo1_oli');
test1.i = 1;
oldList.add(test1);

MyWrapper test2 = new MyWrapper();
test2.s = 'foo2';
test2.oli = new OpportunityLineItem(Description='foo2_oli');
test2.i = 2;
oldList.add(test2);

for (MyWrapper wrapper: oldList) {
    newList.add(new MyWrapper(wrapper));
}

oldList.get(0).oli.Description = 'bar1_oli';
oldList.get(0).s = 'bar1';
oldList.get(0).i = -1;
oldList.get(1).oli.Description = 'bar2_oli';
oldList.get(1).s = 'bar2';
oldList.get(1).i = -2;

System.debug(System.LoggingLevel.ERROR, JSON.serialize(oldList));
System.debug(System.LoggingLevel.ERROR, JSON.serialize(newList));

I get this for oldList

[
  {
    "s": "bar1",
    "oli": {
      "attributes": {
        "type": "OpportunityLineItem"
      },
      "Description": "bar1_oli"
    },
    "i": -1
  },
  {
    "s": "bar2",
    "oli": {
      "attributes": {
        "type": "OpportunityLineItem"
      },
      "Description": "bar2_oli"
    },
    "i": -2
  }
]

and this for newList

[
  {
    "s": "foo1",
    "oli": {
      "attributes": {
        "type": "OpportunityLineItem"
      },
      "Description": "foo1_oli"
    },
    "i": 1
  },
  {
    "s": "foo2",
    "oli": {
      "attributes": {
        "type": "OpportunityLineItem"
      },
      "Description": "foo2_oli"
    },
    "i": 2
  }
]

which is expected