You need to sign in to do that
Don't have an account?
Chris Moynihan
Copying a List of Custom Class
I am building a page to allow entry of multiple records of a custom object (job).
On that page I have a Duplicate button that will add another record by duplicating what is in the last record.
I have a custom class with two elements (record #, job), and I maintain a List of this class, one class for each record on the page.
To duplicate, I create a new instance of my custom class recClass called dupe, then grab the last element in my recClass List, make dupe equal to that element, then increment the record # of dupe to reflect the correct number in the recClass List.
My issue is that when I set the record # to the new value, it also affects the record # of the previous record in the recClass List.
I've read about deepclone, but that seems to apply to sObjects, of which my custom class is not.
Any ideas?
On that page I have a Duplicate button that will add another record by duplicating what is in the last record.
I have a custom class with two elements (record #, job), and I maintain a List of this class, one class for each record on the page.
To duplicate, I create a new instance of my custom class recClass called dupe, then grab the last element in my recClass List, make dupe equal to that element, then increment the record # of dupe to reflect the correct number in the recClass List.
My issue is that when I set the record # to the new value, it also affects the record # of the previous record in the recClass List.
count = count+1; // Create new blank class recClass dupeRecord = new recClass(count); // Hold onto the record # until after the dupe String tempRecCount = dupeRecord.recCount; // Get last record in list, and copy it to the new record dupeRecord = recList[count-2]; system.debug('Duped (copied) record is: '+dupeRecord); system.debug('Original record is: '+recList[count-2]); // This changes the new record, and the original that was duped dupeRecord.recCount = tempRecCount; system.debug('Record to be added: '+dupeRecord); system.debug('Original record is: '+recList[count-2]); // Add duped record to the end of the record list recList.add(dupeRecord);
I've read about deepclone, but that seems to apply to sObjects, of which my custom class is not.
public class recClass { /* recCount acts as a index for a row. Helpful to identify the row to be deleted */ public String recCount {get;set;} public SE_Activities__c seact {get;set;} /* Record Class Constructor */ public recClass(Integer intCount) { recCount = String.valueOf(intCount); /*create a new SE Activity*/ seact = new SE_Activities__c(); } }
Any ideas?
Suraj Gharat
Both the variables (dupeRecord and recList[count-2]) are pointing to same memory location (It means it is just once instance of a class pointed by two variables), and hence it seems when you change recCount of one, the other one also gets changed. Change your code to below, this may resolve.