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
Narmadha Chandrasekar 4Narmadha Chandrasekar 4 

map key not found in map visualforce

I'm trying to display a value from map but getting the error
Map key 0011J00001IxhTrQAJ not found in map
Error is in expression '{!regionMap[par]}'  in page viewaccounthierarchy
Please find my code below:

Class:
public with sharing class ViewAccountHierarchy {
    public Account acc { get; private set; }
    public Account baseAccount { get; private set; }
    public List<Account> parentAccountList { get; private set; }
    public List<Account> grandparentAccountList { get; private set; }
    public Map<Id,list<Account>> parentChildMap { get; private set; }
    public Map<Id,String> parentMap { get; private set; }
    public Map<String,Set<String>> salesAccountMap { get; private set; }
    public Map<Id,String> regionMap { get; private set; }
    public String grandparent{ get; private set; }
    public Id grandparentId{ get; private set; }
    public String baseAccountName{ get; private set; }
    public String baseAccountParent{ get; private set; }
    public ApexPages.StandardController controller { get; private set; }
    
    public ViewAccountHierarchy(ApexPages.StandardController controller) {
        this.acc = (Account)controller.getRecord();
        this.baseAccount = new Account();
        this.parentAccountList = new List<Account>();
        this.grandparentAccountList = new List<Account>();
        this.parentChildMap = new Map<Id,list<Account>>();
        this.parentMap = new Map<Id,String>();
        this.salesAccountMap = new Map<String,Set<String>>();
        this.regionMap = new Map<Id,String>();
        
        Account baseAccount = [SELECT Id,ParentId,Name FROM Account WHERE Id = :acc.id];
        this.baseAccountName = baseAccount.Name;
        this.baseAccountParent = baseAccount.ParentId;
        if(baseAccountParent!=null){
            Account parentAccount = new Account();
            parentAccount = [SELECT Id,ParentId,Name FROM Account WHERE Id = :baseAccount.ParentId];
            parentAccountList.add(parentAccount);
            
            if(parentAccount.ParentId!=null){
                Account grandparentAccount = [SELECT Id,ParentId,Name FROM Account WHERE Id = :parentAccount.ParentId];
                grandparentAccountList.add(grandparentAccount);                
             }else{
                grandparentAccountList.add(parentAccount);
            }
        }else{
            grandparentAccountList.add(baseAccount);
        }
        
        this.grandparent = grandparentAccountList[0].Name;
        this.grandparentid = grandparentAccountList[0].id;
        if(grandparentAccountList.size()>0)
        {
            List<Account> parentAccountList = [SELECT Id,ParentId,Name,Parent.Name,Region__c
                                   FROM Account 
                                   WHERE ParentId = :grandparentAccountList[0].Id];
            if(parentAccountList.size()>0){
                for(integer i=0;i<parentAccountList.size();i++){
                   List<Account> childAccountList = [SELECT Id,ParentId,Name,Parent.Name,YTD_LYB_CM1__c,YTD_LYB_Vol_KGS__c
                                                       FROM Account 
                                                       WHERE ParentId = :parentAccountList[i].Id];
                   List<Sales_Account__c> salesAccountList = [SELECT Id,Seller__c,seller__r.Name
                                                       FROM Sales_Account__c 
                                                       WHERE Sold_To_Account__c = :parentAccountList[i].Id];
                    
                    Set<String> salesAccountSet = new Set<String>();
                    for(integer j=0;j<salesAccountList.size();j++){
                        salesAccountSet.add(salesAccountList[j].seller__r.Name);
                    }
                    parentMap.put(parentAccountList[i].Id,parentAccountList[i].Name);
                    parentChildMap.put(parentAccountList[i].Id,childAccountList);
                    regionMap.put(parentAccountList[i].Id,parentAccountList[i].Region__c);
                    salesAccountMap.put(parentAccountList[i].Id,salesAccountSet);
                }
                
            }
        }
    }
}

VF Page
<apex:repeat value="{!parentMap}" var="par">
  <tr>
   <td> {!regionMap[par]} </td>
   </tr>                   
 </apex:repeat>:

 
Narmadha Chandrasekar 4Narmadha Chandrasekar 4
I passed the entired object into the parentMap and used the same for region. Its fixed. Thanks!