1

I'm trying to iterate over a map of custom apex objects in Visualforce and am unsure how to syntaxicly express it to work.

Scenario that works: map of custom objects

controller:

public Map<Id,cusObj__c> DummyRecordMap{get;set;}

VF

<apex:pageBlockTable value="{!DummyRecordMap}" var="rId" >
         <apex:column >
            <apex:facet name="header">Record Name</apex:facet>
            <apex:outputfield value="{!DummyRecordMap[rId].Name}"/>
        </apex:column>
</apex:pageBlockTable>

Scenario that does not work: map of apex wrapper objects - upd:actually this does work, the flaw was mine and unrelated.

Now, in order to store some extra data per each record in memory I want to use a wrapper apex object as I believe is best practice in such cases. I have done this successfully with List in visualforce in the past, but trying to do this with a map it gives unexpected results. Am I doing something wrong ?

controller:

 public Map<Id,DummyWrapper> DummyWrapperMap{get;set;}

//wrapper class 
public class DummyWrapper{
    public boolean extraField {get;set;}
    public cusObj__c cusObj{get;set;}

    public DummyWrapper(cusObj__c cO){
        extraField = true;
        cusObj = cO;    
    }
}

Visualforce:

<apex:pageBlockTable value="{!DummyWrapperMap}" var="rId" >
         <apex:column >
            <apex:facet name="header">Record Name</apex:facet>
               // --> this does not work like I would have expected <--
            <apex:outputfield value="{!DummyWrapperMap[rId].cusObj.Name}"/>
        </apex:column>
</apex:pageBlockTable>

This does not give errors but displays default values for the correct datatype of the field in the custom object that I try to display. The Name field displays an empty string, a date field the data of today and a decimal displays 0. These fields are unknown to the wrapperobject so the apex:outputfields I try to display them do show the correct datatype, but do not reflect the actual data.

Any ideas ?

(I know that as work around I can tunnel the custom object fields through wrapper object properties, but .. not what I'd prefer to do)

Update: answer to comments

public void buildDummyWrappers(){
    if(DummyRecordMap != null){
            Account ac = [select Id,name from account limit 1]; //TESTING
        DummyWrapperMap = new Map<id,Dummyrapper>();
        for(cusObj__c cO:DummyRecordMap.values()){
            dummyWrapperMap.put(c0.id,new DummyWrapper(co,ac));
        }
    }
}

    //wrapper class 
public class DummyWrapper{
    public boolean extraField {get;set;}
    public cusObj__c cusObj{get;set;}
            public Account Acc{get;set;} //TESTING

    public DummyWrapper(cusObj__c cO, Account ac){
        extraField = true;
        cusObj = cO;
                    Acc = ac;   
    }
}

<apex:repeat value="{!DummyWrapperMap}" var="rId">
         // this works, gives the records Ids (as it should)
         <apex:outputText value="{! rId}"/>
         //this also works fine
          <apex:outputText value="{! DummyWrapperMap[rId].extraField}"/>
         //this does not display anything
         <apex:outputText value="{! DummyWrapperMap[rId].cusObj.Name}"/><br />
         //This does work !
         <apex:outputText value="{! DummyWrapperMap[rId].Acc.Name}"/>
</apex:repeat>

Final update: The problem was in fact in my code. Got to use this.Field = field instead of assuming the compiler will know the difference without this. Case insensitivity.

  • 2
    Not sure if it has something to do with outputField, does it work if you change it to outputText? – manubkk Jun 6 '13 at 9:04
  • If manubkk's suggestion doesn't work, let me know what this produces for you: <apex:repeat value="{!DummyWrapperMap}" var="rId"> {!DummyWrapperMap[rId].extraField}<br/>{!DummyWrapperMap[rId].cusObj.Name}</apex:repeat> – drakored Jun 6 '13 at 9:12
  • Also how are you loading the map class? – drakored Jun 6 '13 at 9:13
  • @RichardTuttle I've updated the question – Samuel De Rycke Jun 6 '13 at 9:33
  • 1
    @Sdry - it's in your code somewhere, works wonders on my end. – drakored Jun 6 '13 at 9:49
2

As mentioned in my comments it seems to be on your side somewhere. Here is a proof of concept on my DE site:

http://ds-developer-edition.na8.force.com/ivr/test

I modified the custom object to be account for the sake of replicating the issue. You can see the account names from a DE org showing through. I put the code inside the page too so you can see exactly how it is functioning on my side. Maybe that will spark an idea on your end. Also of note, I'm using API 27. I saw some problems on 15 (my test page is an old page).

  • I've tried to combine your replication with my code and added an account property to my wrapper .. which does work. Would you mind trying to replicate with a custom object too ? Going a bit crazy here. – Samuel De Rycke Jun 6 '13 at 10:05
  • 1
    Sure, give me a few. We really need a JSFiddle type of application for Apex/VF. Would make quick things like this so much easier. – drakored Jun 6 '13 at 10:06
  • Nevermind. I figured it out. – Samuel De Rycke Jun 6 '13 at 10:07
  • Yep works still. Tested using Milestone__c from the milestones appexchange app. What was the issue? – drakored Jun 6 '13 at 10:09
  • 1
    Ahh, sometimes the biggest pains are the simplest fixes. – drakored Jun 6 '13 at 10:18

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.