Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I seem to be having difficulty getting data from a map back into a list so that it corresponds to the correct Id and field it was originally associated with after it's been modified in a trigger. For some reason, its just not coming to me how to do this efficiently.

This is part of a trigger that creates an event from an opportunity. After inserting the list of new events, here's what I'm trying to do:

   // Iterate through the Save Results
   for(Integer i=0; i<InsertResults.size(); i++){
     If (!InsertResults[i].isSuccess()) oppIdToErrMssg.put( InsertResults[i].Id , 'Error reported was: ' + InsertResults[i].getErrors()[0].getMessage() + '/n' + IdtoEvntNotes.get(InsertResults[i].Id)); 
   }  

   // The above code works. 
   // It appends any save results error messages to the Event_Notes__c field

   // add system debug results to IdtoEvntNotes
   // the loop below, inserts the results from above to an existing map
   // Yes, the two steps could have been combined (something for later)  

  Integer idErrSz = OppIdErrSet.size();

  If (idErrSz != 0) {
     for (Id idz : oppIdToErrMssg.keySet()) {
        IdtoEvntNotes.put(idz, oppIdToErrMssg.get(idz));    
     }

     // here's where I'm having the problem!!!
     // retrieve EvntNotes entries to insert into the original opp list for updating    

     Integer ENts = OppEvntNtsErr.size();
     list<opportunity> EvntNtsErrs = new list<opportunity>();

     // ***** the code below is not correct *****
     // but illustrates what I want to accomplish!
     for ( Integer i=0; i< ENts; i++){
        EvntNtsErrs[i].Id = IdtoEvntNotes.get(IdtoEvntNotes.key[i]);
        EvntNtsErrs[i].Event_Notes__c = IdtoEvntNotes.get(IdtoEvntNotes.key[i]);
     }

     try{
     update OppEvntNtsErr;
        } catch(DmlException e) {
        // Add any exception handling code;
        }

  }

With a keyset(), can one pull out "key[i]"?? or do you have to do an implicit loop that would look something like:

for (Id theKey:theMap.keySet() )
{
   do something with theKey; \\ ex: listname.Id = theKey ??
   do something with theMap.get(theKey); \\ ex: listname.fieldname = theMap.get(theKey) ??
}

It seems as though this should be trivial, but my brain is just mush today. When searching around, I couldn't seem to find anything that provided a solution or example that seemed to be what I needed.

Thanks!

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

The order of insertion is not preserved in a Map, so you wouldn't be able to access it using a numerical index iterator.

Iterate over a list and pull out the corresponding key from the Map KeySet eg

List<Opportunity> oppList = ....

Map<Id, Opportunity> oppMap = ...

for(Opportunity opp : oppList)
System.debug(opp.Id + ' = ' + oppMap.get(opp.Id)); //pull out corresponding element from keyset
share|improve this answer
    
I knew a map didn't preserve order of insertion, but couldn't seem to figure out how to get the Id back so the string would corresponded with the opportunity Id. Thanks ever so much for the assistance!!! My brain can finally rest! –  crmprogdev Feb 20 '13 at 22:27
    
No problem, we can all do with a fresh pair of eyes every now and then :) –  techtrekker Feb 20 '13 at 22:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.