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
Iago FelicioIago Felicio 

How to access in a visualforce page a Map of a Set

My Map looks like this: 
myMap = Map<Id,Set<myObject__c>>
If I try to access the collection of myObject__c by using the following:
<apex:repeat value="{!myMap[exampleofID]}" var="myobject">
     <apex:repeat value="{!myobject}" var="myset">
I get null in myset.

Considerations:
If I access myMap in my controller by using the solution below I access the infos I need. But, I need to access it in my page.
myMap.get(Id).keyset()
Pleeease, someone help me! 
Best Answer chosen by Iago Felicio
LBKLBK
This is what I have done with my Test__c  object.

APEX Code
public class testMapInVF {
    public Map<String, Set<Test__c>> myMap {get; set;}
    public testMapInVF(ApexPages.StandardController controller) {
        myMap = new Map<String, Set<Test__c>>();
        List<Test__c> lTest = [SELECT Id, Name, Number__c FROM Test__c WHERE Product_Support__c  = 'Some'];
        Set<Test__c> sTest  = new Set<Test__c>(lTest);
        myMap.put('Some', sTest);
        List<Test__c> lTest1 = [SELECT Id, Name FROM Test__c WHERE Product_Support__c  != 'Some'];
        Set<Test__c> sTest1  = new Set<Test__c>(lTest1);
        myMap.put('NotSome', sTest1);
    }
}
VF Page
<apex:page standardController="Test__c" extensions="testMapInVF">
    <apex:repeat value="{!myMap['Some']}" var="myobj">
        <apex:outputField value="{!myobj.Name}"/>
        <apex:outputField value="{!myobj.Number__c}"/>
    </apex:repeat>
</apex:page>


 

All Answers

LBKLBK
Hi Iago,

You just need one apex:repeat, because {!myMap[exampleofID]} returns only one set.

Your code will be something like this.
 
<apex:repeat value="{!myMap[exampleofID]}" var="myobject">
	<apex:outputField value ="{!myobject.Name}" />
</apex:repeat>
Let me know if this helps.
 
Harish RamachandruniHarish Ramachandruni
Hi,


Please find below code .

 
<apex:repeat value="{!myMap}" var="m">
	<apex:outputField value ="{!myobject[m].Name}" />
</apex:repeat>

Regards ,
Harish.R.

 
Iago FelicioIago Felicio

@LBK thank you very much for your reply. However, when I try your solution I do get the ID of myobject but when I try to access myobject.Name it gives me an error saying that a String.Name is null.

I'm getting only the ID of the object.

The biggest problem is that there is another part of my project that I do what you suggest and it works. Do you have any idea why this could happen? 

@Harish rao 25, I appreciate your reply as well. I'll still try your solution and later I give a follow up here. 

LBKLBK
Hi Iago,

Unlike the default lists you get in triggers (Trigger.New and Trigger.Old), the SETs and LISTs we fetch in our code carries only the fields that we fetch.

Are you using an SOQL query to populate Set<myObject__c>?

If so, please make sure your SOQL query returns all the fields you would want to use in your VF page.
LBKLBK
This is what I have done with my Test__c  object.

APEX Code
public class testMapInVF {
    public Map<String, Set<Test__c>> myMap {get; set;}
    public testMapInVF(ApexPages.StandardController controller) {
        myMap = new Map<String, Set<Test__c>>();
        List<Test__c> lTest = [SELECT Id, Name, Number__c FROM Test__c WHERE Product_Support__c  = 'Some'];
        Set<Test__c> sTest  = new Set<Test__c>(lTest);
        myMap.put('Some', sTest);
        List<Test__c> lTest1 = [SELECT Id, Name FROM Test__c WHERE Product_Support__c  != 'Some'];
        Set<Test__c> sTest1  = new Set<Test__c>(lTest1);
        myMap.put('NotSome', sTest1);
    }
}
VF Page
<apex:page standardController="Test__c" extensions="testMapInVF">
    <apex:repeat value="{!myMap['Some']}" var="myobj">
        <apex:outputField value="{!myobj.Name}"/>
        <apex:outputField value="{!myobj.Number__c}"/>
    </apex:repeat>
</apex:page>


 
This was selected as the best answer
Iago FelicioIago Felicio

It turns out my real problem was that I didn't give the right permissions to get access to the object! I just needed to change the profile settings of the user I was logged in. 

both ideas would solve my problems! So thanks a lot @LBK and @Harish rao 25.