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
Paul - SalesbriPaul - Salesbri 

Nested Maps - Already Output error.

Hi,

I have a very odd problem indeed.  I am trying to build a nested Map of Contact details.  I need to loop through each Contact record, create a map with the values, Email, AccountId and ContactId and then add this to another map with a integer key.  All pretty straight forward, yet when I try and access the parent map and display the child entries I get the message 'Already Output' and when I loop through the Parent Map object values are all the same, yet when outputting the items in the map after insertion they show the expected values.

The reason I need to use a Map as opposed to a SELECT clause is that we already hold the items in memory and we're trying to limit the number of queries as we're at 11 already.

Code below:
Code:
 private static testMethod void testManualConvertLead()
 {
  Account[] createAccount = new Account[]
   {
    new Account(Name = 'TestLead1'),
    new Account(Name = 'TestLead1'),
    new Account(Name = 'TestLead1')
   };
   
  insert createAccount;

  Contact[] createContact = new Contact[]
   {
    new Contact(FirstName = 'Test', LastName = 'Last 1', Email = 'test1@bob.com', AccountId = createAccount[0].Id),
    new Contact(FirstName = 'Test', LastName = 'Last 2', Email = 'test2@bob.com', AccountId = createAccount[1].Id),
    new Contact(FirstName = 'Test', LastName = 'Last 3', Email = 'test3@bob.com', AccountId = createAccount[2].Id)
   };
   
  insert createContact; 

  Map<Integer,Map<String,String>> contactDetails = new Map<Integer,Map<String,String>>();
  Map<String,String> contactDetail = new Map<String,String>();
  system.debug(createContact.size()); 
  for(integer n = 0; n < createContact.size() ; n++)
  {
   system.debug('-');
   system.debug(createContact[n].Id);
   system.debug(createContact[n].AccountId);
   system.debug(createContact[n].Email);
   
   contactDetail.put('ContactId',createContact[n].Id);
   contactDetail.put('AccountId',createContact[n].AccountId);
   contactDetail.put('Email',createContact[n].Email);
   
   system.debug(contactDetail);   
   system.debug(n);
   
   contactDetails.put(n,contactDetail);
   system.debug(contactDetails.get(n));
   system.debug('-');
  }
  
  system.debug(contactDetails);
  
  for(integer p = 0 ; p < contactDetails.size() ; p++)
  {
   system.debug(contactDetails.get(p));
  }
 }

 

kevinvwkevinvw
Hi Paul, I am having the same issue.  Did you ever find a solution for this?
PaulATPimptastiPaulATPimptasti

Hi Kevin,

 

I'm afraid not.  It is such a long time ago, but I do remember seeing this issue again.  However, it only appeared to be an issue when sending to the debug log.  The actual data is still in the Map.

 

Hope that helps.

 

Paul 

kevinvwkevinvw

Hi Paul,  I figured my issue out.  My map actually was messed up even when I read the data out into a table. 

I had both my map definition defined outside the main loop.

I had to move my inner map definition inside the main loop and then it worked.

 

I was playing with this example when I realized what was wrong with my code -

http://community.salesforce.com/sforce/board/message?board.id=apex&message.id=23133

 

Thanks,  Kevin.

 

DixitSDixitS
Hi There,

I also faced same issue but in a different scenario.

After going through blogs & docs, I get to know that if I call some method like clear() on a list and put it in a map and use the same variable with modified values for next element of map, ultimately SFDC stores address of the same variable in each element of map, because of which I finally get same value in each element of map .

Thanks, Shambhvi