+ Start a Discussion
Fowkes1985Fowkes1985 

Help please: Map key xxxx not found in map

I have added a VF page, trigger and control and receive Map key a2VD0000000v4AnMAI not found in map.

Any help at all please, new to Apex and using similar development work as a template

VF Page:
<apex:page controller="CampItineraryController" title="Camp Itinerary">
<apex:sectionHeader title="Camp Itinerary"/>
<apex:pageMessages />
<apex:form >    
  <apex:pageBlock title="Sessions">

        <apex:pageBlockButtons >
          <apex:commandButton action="{!saveSessions}" id="saveButton" value="Save" status="sessionsStatus" rerender="elementsTable"/>
          <apex:commandButton action="{!addSessions}"  id="addButton"  value="Add" rerender="sessionTable, elementsTable" status="sessionsStatus"/>
          <apex:commandButton action="{!deleteSessions}"  id="deleteButton"  value="Delete" rerender="sessionTable" status="sessionsStatus"/>
        </apex:pageBlockButtons>
        <apex:actionStatus id="sessionsStatus" stopText="">
            <apex:facet name="start" >
              <apex:outputPanel >
              <apex:outputtext value="Updating...    "/>
              <apex:image url="/img/loading.gif" />                       
              </apex:outputPanel>
            </apex:facet>          
          </apex:actionStatus>         
        <apex:pageBlockTable value="{!sessions}" var="r" id="sessionTable">   
          <apex:column headerValue="Select" style="width: 30px">        
            <apex:inputCheckbox value="{!r.isSelected}"/>
          </apex:column>
          <apex:repeat value="{!$ObjectType.Session__c.FieldSets.Related_List_Fields}" var="f">
            <apex:column headerValue="{!f.label}" >
              <apex:inputField value="{!r.obj[f]}" />
            </apex:column>
          </apex:repeat>
        </apex:pageBlockTable>
  </apex:pageBlock>

  <apex:pageBlock title="Durations">

        <apex:pageBlockButtons >
          <apex:commandButton action="{!saveDurations}" id="saveButton" value="Save" status="durationsStatus"/>
          <apex:commandButton action="{!addDurations}"  id="addButton"  value="Add" rerender="durationTable" status="durationsStatus"/>
          <apex:commandButton action="{!deleteDurations}"  id="deleteButton"  value="Delete" rerender="durationTable" status="durationsStatus"/>
        </apex:pageBlockButtons>
        <apex:actionStatus id="durationsStatus" stopText="">
            <apex:facet name="start" >
              <apex:outputPanel >
              <apex:outputtext value="Updating...    "/>
              <apex:image url="/img/loading.gif" />                       
              </apex:outputPanel>
            </apex:facet>          
          </apex:actionStatus>         
        <apex:pageBlockTable value="{!durations}" var="d" id="durationTable">   
          <apex:column headerValue="Select" style="width: 30px">        
            <apex:inputCheckbox value="{!d.isSelected}"/>
          </apex:column>
          <apex:repeat value="{!$ObjectType.Session_Duration__c.FieldSets.Related_List_Fields}" var="f">
            <apex:column headerValue="{!f.label}" >
              <apex:inputField value="{!d.obj[f]}" />
            </apex:column>
          </apex:repeat>
        </apex:pageBlockTable>
  </apex:pageBlock>

  <apex:pageBlock title="Elements">

        <apex:pageBlockButtons >
          <apex:commandButton action="{!saveElements}" id="saveButton" value="Save" status="elementsStatus"/>
        </apex:pageBlockButtons>
        
<!--        <apex:repeat value="{!sessions}" var="thisSession">
            <apex:repeat value="{!durations}" var="thisDuration">
                {!thisSession.obj.id} {!thisDuration.obj.id} <br/>
                {!elements[thisSession.obj.id][thisDuration.obj.id].Element__c} <br/>
            </apex:repeat>
        </apex:repeat>-->

        <apex:pageBlockTable value="{!sessions}" var="thisSession" id="elementsTable">
                <apex:column headerValue="Session">
                    <apex:outputField value="{!thisSession.obj['name']}"/>
                </apex:column>
            <apex:repeat value="{!durations}" var="thisDuration">
                <apex:column headerValue="{!thisDuration.obj['name']}">
                    <apex:inputField value="{!elements[thisSession.obj.id][thisDuration.obj.id].Element__c}"/>
                </apex:column>
            </apex:repeat>
        </apex:pageBlockTable>
  </apex:pageBlock>  

</apex:form>
</apex:page>

Controller
 
ublic with sharing class CampItineraryController {

    public List<sObject> extractObjs(List<sObjectWithSelect> theList) {
        return extractObjs(theList, false);
    }
    
    public List<sObject> extractObjs(List<sObjectWithSelect> theList, boolean testForSelected) {
        List<sObject> rval = new List<sObject>();
    
        for(sObjectWithSelect thisObj : theList) {
            if(!testForSelected || thisObj.isSelected) {
                rval.add(thisObj.obj);
            }
        }
        
        return rval;
    }

    public class sObjectWithSelect {
        
        public sObject obj {get; set;}
        public boolean isSelected {get; set;}
        
        public sObjectWithSelect(sObject obj) {
            this.obj = obj;
            this.isSelected = false;
        }
        
    }

    public List<sObjectWithSelect> sessions {get; set;}
    public List<sObjectWithSelect> durations {get; set;}
    public Map<String, Map<String, Session_To_Duration__c>> elements {get; set;}
    private List<Session_To_Duration__c> rawelements;
    private String sessionQueryString;
    private String durationQueryString;
    
    public CampItineraryController() {
        Schema.FieldSet fs = Schema.SObjectType.Session__c.fieldSets.Related_List_Fields;
        
        String selectfields = 'id ';
        
        for(Schema.FieldSetMember thisField : fs.getFields()) {
            selectFields = selectFields + ', ' + thisField.getFieldPath();
        }

        sessionQueryString = 'SELECT ' + selectfields
                         + ' FROM Session__c ORDER BY Name ASC';

        
        fs = Schema.SObjectType.Session_Duration__c.fieldSets.Related_List_Fields;
        
        selectfields = 'id ';
        
        for(Schema.FieldSetMember thisField : fs.getFields()) {
            selectFields = selectFields + ', ' + thisField.getFieldPath();
        }

        durationQueryString = 'SELECT ' + selectfields
                         + ' FROM Session_Duration__c ORDER BY Name ASC';        

        initSessions();
        initDurations();
        initElements();        
    }
    
    private void initElements() {
        rawElements = [SELECT id, Element__c, Session__c, Session_Duration__c 
                                               FROM Session_To_Duration__c];
                                               
        elements = new Map<String, Map<String, Session_To_Duration__c>>();
        
        for(Session_To_Duration__c thisSTD : rawElements) {
            Map<String, Session_To_Duration__c> thisMap = elements.get(thisSTD.Session__c);
            
            if(thisMap == null) {
                thisMap = new Map<String, Session_To_Duration__c>();
                elements.put(thisSTD.Session__c, thisMap);
            }
            
            thisMap.put(thisSTD.Session_Duration__c, thisSTD);
        }
    }
    
    private void initSessions() {
        system.debug(sessionQueryString);
        List<Session__c> theSessions = Database.query(sessionQueryString);

        sessions = new List<sObjectWithSelect>();

        for(Session__c thisSession : theSessions) {
            sessions.add(new sObjectWithSelect(thisSession));
        }
    }

    private void initDurations() {
        system.debug(durationQueryString);
        List<Session_Duration__c> theDurations = Database.query(durationQueryString);

        durations = new List<sObjectWithSelect>();

        for(Session_Duration__c thisDuration : theDurations) {
            durations.add(new sObjectWithSelect(thisDuration));
        }
    }
        
    public PageReference addSessions() {
        saveSessions();
        Session__c newSession = new Session__c(Name = 'Enter a session name');
        
        try {
            insert newSession;
        } catch(Exception e) {
            ApexPages.addMessages(e);
        }
        
        sessions.add(new sObjectWithSelect(newSession));
        initElements();
        
        return null;
    }

    public PageReference saveSessions() {
        
        try {
            update extractObjs(sessions);
        } catch(Exception e) {
            ApexPages.addMessages(e);
        }
        
        initElements();
        
        return null;
    }

    public PageReference deleteSessions() {
        
        try {
            delete extractObjs(sessions, true);
        } catch(Exception e) {
            ApexPages.addMessages(e);
        }
        
        initSessions();
        initElements();
        
        return null;
    }
    public PageReference deleteDurations() {
        try {
            delete extractObjs(durations, true);
        } catch(Exception e) {
            ApexPages.addMessages(e);
        }
        
        initDurations();
        initElements();
        
        return null;
    }


    public PageReference addDurations() {
        saveDurations();
        Session_Duration__c newDuration = new Session_Duration__c(Name = 'Enter a duration name');
        
        try {
            insert newDuration;
        } catch(Exception e) {
            ApexPages.addMessages(e);
        }
        
        durations.add(new sObjectWithSelect(newDuration));
        initElements();
       return null;
    }


    public PageReference saveDurations() {
        try {
            update extractObjs(durations);
        } catch(Exception e) {
            ApexPages.addMessages(e);
        }
        
        initDurations();
        initElements();
        
        return null;
    }

    public PageReference saveElements() {
        try {
            update rawElements;
        } catch(Exception e) {
            ApexPages.addMessages(e);
        }
        
        return null;
    }

}

 
Mike.KatulkaMike.Katulka
My guess would be line 68 and/or 78 in the visualforce code. Where you are asking from the "elements" map by some keys.  Debug the values both on the screen (and comment out the VF code for the "elements" map) and also do a system debug in apex of the values that you are pumping into the elements map upon initialization.  You could be missing something in the controller perhaps.