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
fadwa mangougfadwa mangoug 

Display Apex maps in Visualforce page

Hey,

Im trying to display maps in a visualforce page but the method that builds the map doesnt seem to be called when i preview my page. I would appreciate some help please :)

Here's my map : 

public map<string,string> getMapFieldLabel(){
        
        //List<string> fieldsLabels = new List<string>();
        List<Custom_Object__c> defaultFields = [Select Name from Custom_Object__c where Default__c=True];
        Map<String, Schema.SObjectField> typeMap = Schema.SObjectType.A_second_Custom_Object__c.fields.getMap();
          
        //Get choosen default fields label    
        for(Custom_Object__c defaultField : defaultFields) {
            
            Schema.SObjectField field = typeMap.get(defaultField.Name);
            string fieldLabel =field.getDescribe().getLabel();
            mapFieldLabel.put(defaultField.Name, fieldLabel);
            //fieldsLabels.add(fieldLabel);
            
            
        }
        system.debug(mapFieldLabel);
        return mapFieldLabel;
        
    }

And my VF code : 

         <apex:pageBlockSection columns="3" collapsible="true">
                
            <apex:repeat value="{!MapFieldLabel}" var="item" >
                    
                    <apex:repeat value="{!MapFieldLabel[item]}" var="itemvalue" >                    
                    <apex:outputText value="{!itemvalue}" />
                    </apex:repeat>
            </apex:repeat>   
            </apex:pageBlockSection>

This code doesnt display anything on screen !

Thank you for your help in advance :)
Best Answer chosen by fadwa mangoug
GulshanRajGulshanRaj
Hi

New code is more destructive:

Please follow step and let me know:
1) Take this code:
public map<string,string> getMapFieldLabel(){
        Map<String,String> mapField_Label = new Map<String,String>()
        //List<string> fieldsLabels = new List<string>();
        List<Custom_Object__c> defaultFields = [Select Name from Custom_Object__c where Default__c=True];
        Map<String, Schema.SObjectField> typeMap = Schema.SObjectType.A_second_Custom_Object__c.fields.getMap();
          
        //Get choosen default fields label    
        for(Custom_Object__c defaultField : defaultFields) {
            
            Schema.SObjectField field = typeMap.get(defaultField.Name);
            string fieldLabel =field.getDescribe().getLabel();
            mapField_Label.put(defaultField.Name, fieldLabel);
            //fieldsLabels.add(fieldLabel);
            
            
        }
        system.debug(mapField_Label);
        return mapField_Label;
        
    }
2) In VF page:
<apex:pageBlockSection columns="3" collapsible="false">
                
            <apex:repeat value="{!MapFieldLabel}" var="item" >
                   <apex:outputText value="{!MapFieldLabel[item]}" />
            </apex:repeat>   
            </apex:pageBlockSection>
3) Check in debug log if data is coming in mapField_Label variable.




Thanks
Gulshan Raj

 

All Answers

GulshanRajGulshanRaj
Hi,

You are trying to iterating Schema.SObjectField in the inner repeat which is not list or iterable.

Here is code which will help you  to understand where you are wrong:
<apex:pageBlockSection columns="3" collapsible="true">
            <apex:repeat value="{!MapFieldLabel}" var="item" >
                    <!-- item contains key value which is string. and when you call  MapFieldLabel[item]                      it returns respective  field label. So, it's return single value which is not iterable inside repeat.

                    // your code
                    -->                  
                    
                    </apex:repeat>
            </apex:repeat>   
            </apex:pageBlockSection>

I am expecting more question as it look like you wanted to achieve something inside inner repeat. Please let me know if I can help you.


Thanks
Gulshan Raj

 
JeffreyStevensJeffreyStevens
<apex:pageBlockSection columns="3" collapsible="true">
                
            <apex:repeat value="{!MapFieldLabel}" var="item" >
                    MapKey={!item}<br/>
                    MapValue={!MapFieldLabel[item]}<br/>
            </apex:repeat>   
            </apex:pageBlockSection>

Try that - I think you'll see the keys and values of the map 
fadwa mangougfadwa mangoug
Hey
@Gulshan Raj11
I tried to convert the Schema.SObjectField Label to a string and to use a Map of lists to iterate over them inn my VF but still not working : 
 
public Map<List<string>,List<string>> getMapFieldLabel(){
        
        system.debug('In the getMapfieldLabel');        
        
        List<string> fieldsLabels = new List<string>();
        List<string> fieldsNames = new List<string>();
        
        List<Proximity_Search_Engine__c> defaultFields = [Select Name from Custom_Object__c where Default__c=True];
        Map<String, Schema.SObjectField> typeMap = Schema.SObjectType.Second_Custom_Object__c.fields.getMap();
          
        //Get choosen default fields label    
        for(Custom_Object__c defaultField : defaultFields) {
            fieldsNames.add(string.ValueOf(defaultField));
            Schema.SObjectField field = typeMap.get(defaultField.Name);
            string fieldLabel = string.valueOF(field.getDescribe().getLabel());
            fieldsLabels.add(fieldLabel);
        }
        mapFieldLabel.put(fieldsNames, fieldsLabels);
        system.debug(mapFieldLabel);
        return mapFieldLabel;
        
    }
When executing the class, the method is not even called...

@JeffreyStevens,
Thanks for the quick answer !
I tried the code u suggested but still not displaying anything.
GulshanRajGulshanRaj
Hi

New code is more destructive:

Please follow step and let me know:
1) Take this code:
public map<string,string> getMapFieldLabel(){
        Map<String,String> mapField_Label = new Map<String,String>()
        //List<string> fieldsLabels = new List<string>();
        List<Custom_Object__c> defaultFields = [Select Name from Custom_Object__c where Default__c=True];
        Map<String, Schema.SObjectField> typeMap = Schema.SObjectType.A_second_Custom_Object__c.fields.getMap();
          
        //Get choosen default fields label    
        for(Custom_Object__c defaultField : defaultFields) {
            
            Schema.SObjectField field = typeMap.get(defaultField.Name);
            string fieldLabel =field.getDescribe().getLabel();
            mapField_Label.put(defaultField.Name, fieldLabel);
            //fieldsLabels.add(fieldLabel);
            
            
        }
        system.debug(mapField_Label);
        return mapField_Label;
        
    }
2) In VF page:
<apex:pageBlockSection columns="3" collapsible="false">
                
            <apex:repeat value="{!MapFieldLabel}" var="item" >
                   <apex:outputText value="{!MapFieldLabel[item]}" />
            </apex:repeat>   
            </apex:pageBlockSection>
3) Check in debug log if data is coming in mapField_Label variable.




Thanks
Gulshan Raj

 
This was selected as the best answer
fadwa mangougfadwa mangoug
Hey @Gulsha Raj 11

Thank so much you for ur answers. So the apex code worked and the map is now populated and the method called, but, the VF is still not displaying the map data.

I appreciate the help thank you !

Fadwa
GulshanRajGulshanRaj
Can you please share what value coming inside variable mapField_Label in debug log. I am sure you are very close.

Thanks
Gulshan Raj
fadwa mangougfadwa mangoug
@GulshanRaj, 

It finally worked, i've put the maps in my constructor and it displayed the values well in my VF.

Thank you for the help :)

Fadwa
GulshanRajGulshanRaj
That's good news.

Can you please mark this question as solved and choose best answer so it will help other in future to choose best solution. 

Thanks
Gulshan Raj
Ashish Kumar YadavAshish Kumar Yadav
Hi Team,

how to display element count in vf page  if I am trying to display it while click on show product method value of quantity increasing can you help me .
visualforce page i am displaying like that but problem is if i click on pagination first page,last page method is calling so value is increasing so what i need to do.

vf page
======
 <input type="hidden" id='{!a.pricebookentry.Product2.name}q' value="{!elementCount[a.pricebookentry.EN_Code__c]}"/>

Apex class

============
I have added this code in method showproductdata()
 elementCount=new map<string,Integer>();
                    List<String> filterLogicSplittedbySpace = (List<String>)System.JSON.deserialize(barcodeIn, List<String>.class);
                    system.debug('filterLogicSplittedbySpace'+ filterLogicSplittedbySpace.size());
                    for (String str : filterLogicSplittedbySpace){
                        system.debug('str'+str);
                        str =str.trim();
                        elementList.add(str);
                    }
                    system.debug('elementList size'+elementList.size());
                    for(String key : elementList)
                    {
                        system.debug('elementCount.containsKey(key)'+ elementCount.containsKey(key));
                        
                        if(!elementCount.containsKey(key)){
                            elementCount.put(key,0);
                        }
                        
                        currentInt=elementCount.get(key)+1;
                        System.debug('currentInt@@'+currentInt);
                        elementCount.put(key,currentInt);
                        
                    }