0

I am iterating map in pageBlockTable inside of which I have used apex:repeat. An error is coming.

public class CreateEditAccount
{
    public Account acc{get;set;}
    public Contact con{get;set;}
    public List<Contact> conList{get;set;}
    public List<Account> accList{get;set;}
    public Integer index{get;set;}
    public string accIdFromPage{get;set;}
    public map<Integer,OuterWrapper> intVsOuterWrapperMap{get;set;}
    public map<Integer,InnerWrapper> intVsInnerWrapperMap{get;set;}
    public CreateEditAccount()
    {
        acc = new Account();
        con = new Contact();
        conList = new List<Contact>();
        intVsOuterWrapperMap = new map<Integer,OuterWrapper>();
        intVsInnerWrapperMap = new map<Integer,InnerWrapper>();
        accList = [Select id, name, website, PageInfoFlag__c, BillingCountry, (select id, accountId, firstname, lastname, MobilePhone, email From contacts) From Account Where PageInfoFlag__c = true Order By LastModifiedDate DESC];
        index = 0;
        for(Account acc : accList)
        {
            Integer contactIndex = 0;
            OuterWrapper outerObj = new OuterWrapper();
            List<InnerWrapper> innerList = new List<InnerWrapper>();
            for(Contact con : acc.contacts){
                InnerWrapper innerObj = new InnerWrapper();
                innerObj.cont = con;
                innerObj.rowToRemove = contactIndex;
                innerList.add(innerObj);
                contactIndex++;
            }
            outerObj.innerContactList = innerList;
            outerObj.acct = acc;
            intVsOuterWrapperMap.put(index, outerObj);
            index++;
        }
        system.debug('intVsOuterWrapperMap = '+intVsOuterWrapperMap);
    }


    public PageReference Save()
    {
        return null;
    }
    public PageReference DeleteAccount()
    {
        return null;
    }

    public PageReference saveGridData()
    {
        return null;
    }

    public class OuterWrapper
    {
        public Account acct{get;set;}
        public List<InnerWrapper> innerContactList{get;set;}
        public OuterWrapper()
        {
            innerContactList = new List<InnerWrapper>();
            acct = new Account();
        }
    }

    public class InnerWrapper
    {
        public Integer rowToRemove{get;set;}
        public Contact cont{get;set;}
        public InnerWrapper()
        {
            cont = new Contact();
        }
    }
}

Page:

<apex:page controller="CreateEditAccount" tabStyle="Contact">
    <apex:form>
        <apex:pageBlock title="Form Detail" id="myForm">
            <apex:pageBlockSection title="Account Detail">
                <apex:inputField value="{!acc.Name}" id="textfield1"/>
                <apex:inputField value="{!acc.Website}" id="textfield2"/>
                <apex:inputField value="{!acc.BillingCountry}" id="textfield3"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Contact Detail">
                <apex:inputField value="{!con.FirstName}" id="textfield4"/>
                <apex:inputField value="{!con.LastName}" id="textfield5"/>
                <apex:inputField value="{!con.MobilePhone}" id="textfield6"/>
                <apex:inputField value="{!con.Email}" required="true" id="textfield7"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="Bottom">
                <!--apex:commandButton value="Save" action="{!Save}" reRender="out" oncomplete="clearFields();"/>
                <apex:commandButton value="Save & New" action="{!SaveAndNew}" reRender="out" oncomplete="clearFields();"/>
                <apex:commandButton value="Reset" immediate="true" reRender="myForm" onclick="clearFields();"/-->
            </apex:pageBlockButtons>
            <apex:pageBlockSection >
                <apex:pageBlockTable var="val" value="{!intVsOuterWrapperMap}">
                        <apex:column headerValue="Name">
                            <apex:outputField value="{!intVsOuterWrapperMap[val].acct.Name}">
                                <apex:inlineEditSupport event="ondblclick" showOnEdit="up,cn" hideOnEdit="ed,del"/>
                            </apex:outputField>
                        </apex:column>
                        <apex:column headerValue="Website">
                            <apex:outputField value="{!intVsOuterWrapperMap[val].acct.Website}">
                                <apex:inlineEditSupport event="ondblclick" showOnEdit="up,cn" hideOnEdit="ed,del"/>
                            </apex:outputField>
                        </apex:column>
                        <apex:column headerValue="BillingCountry">
                            <apex:outputField value="{!intVsOuterWrapperMap[val].acct.BillingCountry}">
                                <apex:inlineEditSupport event="ondblclick" showOnEdit="up,cn" hideOnEdit="ed,del"/>
                            </apex:outputField>        
                        </apex:column>

                        <apex:column headerValue="Actions">
                            <apex:commandButton value="Create Contact" onclick="PrefillAccount('{!acc.name}','{!acc.Website}','{!acc.billingCountry}','{!acc.Id}');" id="createcontact" immediate="true" reRender="out">
                                <apex:param assignTo="{!accIdFromPage}" value="{!acc.Id}" name="par"/>
                            </apex:commandButton>
                            <apex:commandButton value="Edit" id="ed"/>
                            <a id="del" href="javascript: if (window.confirm('Are you sure?')) DeleteAccount('{!acc.Id}');">Delete</a>

                            <apex:commandButton value="Update" action="{!saveGridData}" id="up"/>

                            <apex:commandButton value="Cancel" onclick="clearFields();" id="cn"/>

                        </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            <apex:pageBlockSection>
                <apex:pageBlockTable var="key" value="{!intVsOuterWrapperMap}">
                    <apex:repeat var="val" value="{!intVsOuterWrapperMap[key].innerContactList}">
                        <apex:column headerValue="Website">
                            <apex:outputText value="{!val.cont.firstname}"/>
                        </apex:column>
                            <!--apex:outputText value="{!val.cont.lastname}"/>
                            <apex:outputText value="{!val.cont.email}"/>
                            <apex:outputText value="{!val.cont.mobilephone}"/-->
                    </apex:repeat>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
    <script type="text/javascript">
        <apex:repeat var="key" value="{!intVsOuterWrapperMap}">
           <apex:repeat var="val" value="{!intVsOuterWrapperMap[key].innerContactList}">

           </apex:repeat>
        </apex:repeat>
        function clearFields() {
            document.getElementById("j_id0:j_id1:myForm:j_id2:textfield1").value="";
            document.getElementById("j_id0:j_id1:myForm:j_id2:textfield2").value="";
            document.getElementById("j_id0:j_id1:myForm:j_id2:textfield3").value="";
            document.getElementById("j_id0:j_id1:myForm:j_id3:textfield4").value="";
            document.getElementById("j_id0:j_id1:myForm:j_id3:textfield5").value="";
            document.getElementById("j_id0:j_id1:myForm:j_id3:textfield6").value="";
            document.getElementById("j_id0:j_id1:myForm:j_id3:textfield7").value="";
        }
        function PrefillAccount(name, website, country, accId){
            accId = accId;
            document.getElementById("j_id0:j_id1:myForm:j_id2:textfield1").value=name;
            document.getElementById("j_id0:j_id1:myForm:j_id2:textfield2").value=website;
            document.getElementById("j_id0:j_id1:myForm:j_id2:textfield3").value=country;
            document.getElementById("j_id0:j_id1:myForm:j_id3:textfield4").value="";
            document.getElementById("j_id0:j_id1:myForm:j_id3:textfield5").value="";
            document.getElementById("j_id0:j_id1:myForm:j_id3:textfield6").value="";
            document.getElementById("j_id0:j_id1:myForm:j_id3:textfield7").value="";
        }
    </script> 
</apex:page>

Map key null not found in map Error is in expression '{!intVsOuterWrapperMap[key].innerContactList}' in component in page createeditaccount

  • Try to rename key to keyvar in your apex:pageBlockTable – Tushar Sharma Jun 24 '17 at 13:03
  • @TusharSharma thanks for the reply. But same error is coming – Anzar Jun 24 '17 at 13:11
  • @TusharSharma When I am using repeat instead of pbt it is working but the ui is getting distorted – Anzar Jun 24 '17 at 13:13
  • wouldn't it just be val.firstname? – Logan Jun 24 '17 at 16:08
0

You can use a list instead of map since you are not using the key as far as I can tell.

<apex:pageBlockTable var="outer" value="{!wrapperList}">
    <apex:repeat var="val" value="{!outer.innerContactList}">
        <apex:column headerValue="Website">
            <apex:outputText value="{!val.cont.firstname}"/>
        </apex:column>
            <!--apex:outputText value="{!val.cont.lastname}"/>
            <apex:outputText value="{!val.cont.email}"/>
            <apex:outputText value="{!val.cont.mobilephone}"/-->
    </apex:repeat>
</apex:pageBlockTable>

In the controller just:

//intVsOuterWrapperMap.put(index, outerObj);
wrapperList.add(outerObj);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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