I'm working on a class thats outside the normal trigger set up and used as part of an integration.
I'm looking to have two list or maps, one that contains the records data pre update. The second containing the records with the updated data. My own version of old and new list/map.
A simple example of the code and issue:
List<Account> accListOne = [SELECT name, Email__c
FROM Account
WHERE Email__c != null LIMIT 1];
system.debug('*** PRE UPDATE accListOne *** ' + accListOne );
system.debug('*** PRE UPDATE accListOne - email *** ' + accListOne[0].Email__c );
List<Account> accListTwo = new List<Account>(accListOne);
system.debug('*** PRE UPDATE accListTwo *** ' + accListTwo );
system.debug('*** PRE UPDATE accListTwo - email *** ' + accListTwo[0].Email__c );
for(account a : accListOne) {
a.Email__c = '[email protected]';
}
system.debug('*** POST UPDATE accListOne *** ' + accListOne );
system.debug('*** POST UPDATE accListOne - email *** ' + accListOne[0].Email__c );
system.debug('*** POST UPDATE accListTwo *** ' + accListTwo );
system.debug('*** POST UPDATE accListTwo - email *** ' + accListTwo[0].Email__c );
As you can see above when I perform the update on the record the email is being updated in both lists.
Whats the best way to stop this happening so the update only appears in accListTwo?
Thanks