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
Steve BerleySteve Berley 

id field in list changing as contents added

I have a method that loops through one map and adds relevant information from a another map.  
for(id opp : opps.keySet()){
  id dg = opps.get(opp);
  if (templates.containsKey(dg) ) {
    list<npsp__Allocation__c> thisTemplate = templates.get(dg);
    for (npsp__Allocation__c t : thisTemplate) {
      t.npsp__Opportunity__c = opp;
      toInsert.add(t);
      system.debug('added: '+toInsert[toInsert.size()-1].npsp__Opportunity__c);  // << #1
    }
  }
}
 
system.debug('------------------ ');
for (npsp__Allocation__c a : toInsert ) system.debug(a.npsp__Opportunity__c);   // << #2

Looking at the output from the debug statements - the two ids are different, specificially one includes FF and the other includes FG.  However when interrogated again they're both FG.

User-added image

What's going on?  How do I fix this?  I have tried changing API version of the code - no luck.  
Steve BerleySteve Berley
in case others run into this type of issue in the future - the problem boiled down to passing values by reference vs. by value.
t.npsp__Opportunity__c = opp;
toInsert.add(t);

instead of above - it was solved by createing a new npsp__Allocation__c and copying values to it, then adding it.