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
AngrySlothAngrySloth 

Apex:Repeat with a Map and actionFunction rerender no longer working properly

I have some code which has been working for almost a year which has suddenly stoped working. It involves creating a Map<String, Wrapper> called selectedItems in the apex controller, using apex:repeat to iterate over the object in visualforce. There is a check box which when check or unchecked fires actionFunction to to the apex controller to either remove or add the item to the selectedItems Map and then rerenders the apex:outputPanel where the selectedItems is displayed.  The following is something i threw together to duplicate the problem as the actual code is much more involved and this is the problem I am facing.

Apex Controller
public with sharing class TestMapError {

  public Map<Id, Product2> prodMap { get; set; }
  public Map<Id, Product2> selectedMap { get; set; }

	public TestMapError() {
    prodMap = new Map<Id, Product2>([SELECT Id, Name FROM Product2 WHERE isActive = true LIMIT 5]);
    selectedMap = new Map<Id, Product2>();
	}

  public void toggleSelect()
  {
    String pId = Apexpages.currentPage().getParameters().get('pId');
    if( selectedMap.containsKey(pId) )
    {
      selectedMap.remove(pId);
    }
    else
    {
      selectedMap.put(pId, prodMap.get(pId) );
    }
  }

}

Visualforce Page
 
<apex:page showHeader="true" sidebar="true" controller="TestMapError">
  <apex:includeScript value="{!URLFOR($Resource.jQuery)}"/>

  <apex:form>

    <apex:actionFunction name="toggleSelected"
                         action="{!toggleSelect}"
                         rerender="selectedList">
      <apex:param name="pId" value="" />
    </apex:actionFunction>

    <apex:outputPanel id="theList">
      <ul>
        <apex:repeat value="{!prodMap}" var="prodId">
          <li>
            {!prodMap[prodId].Name}
            <input type="checkbox" data-pid="{!prodId}" onChange="toggleCheck(this);"/>
          </li>
        </apex:repeat>
      </ul>
    </apex:outputPanel>

  </apex:form>
  <br />

  <apex:outputPanel id="selectedList">
    <h3>Selected</h3>
     <ul>
      <apex:repeat value="{!selectedMap}" var="sId">
        <li>{!selectedMap[sId].Name}</li>
      </apex:repeat>
    </ul>
  </apex:outputPanel>

  <script type="text/javascript">

  var $j = jQuery.noConflict();


    function toggleCheck(ele)
    {
      console.log( $j(ele).data('pid'));
      toggleSelected( $j(ele).data('pid') );
    }

  </script>

</apex:page>

When the check box is selected, the first Item is not rendered in the selected list, but if you select a second item the first shows up.  If you check the view state, both items are present in the selectedMap.  Then if you uncheck any of the checked boxes you get a visualforce error of Map key not found in Map.

This used to work up until very recently and I did recieve an email from Salesforce stating they are upgrading to JDK 8.  Is it possible this is the underlying problem? Any help on this would be greatly appreciated
AngrySlothAngrySloth
Thanks for the reply Ajay, but that is infact not the problem. I was receiving a visualforce error Of map key not found in map selectedMap, and that was only when removing, or unchecking the check box.  It had to do with the way visualforce was rerendering the selectedMap after the toggleSelect method was called by actionFunction. It just did not work as advertised.  I was incontact with Salesforce support, and although I have not heard back from them, the above code now function perfectly fine without me changing a thing.