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!
