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
theitdeptrockstheitdeptrocks 

Dynamic Get

Hey all,

 

Would it be possible to do something like this:

    public map<string,integer> field1 = new map<string,integer>();
    public map<string,integer> field2 = new map<string,integer>();

    public test3(ApexPages.StandardController controller) {
        this.field1.put('a',1);
        this.field2.put('b',2);
    }
    
    public integer getx(string x){
        return field2.get(x);
    }

 I'm just looking to be able to have something on the VF side like {!x[a]} to return 1.  Possible?

 

Thanks!

bob_buzzardbob_buzzard

If you use dynamic visualforce bindings, you should be able to access a value from the map directly by its key without recourse to a getter.

 

You'll need to make the maps properties, but aside from that there's not much to change:

 

Controller:

 

 public map<string,integer> field1 {get; set;}    
 public map<string,integer> field2 {get; set;}

    public test3(ApexPages.StandardController controller) {
        field1 = new map<string,integer>();
        field2 = new map<string,integer>();

        this.field1.put('a',1);
        this.field2.put('b',2);
    }
    

 

and page:

 

<apex:outputText value="{!field2['a']}" />

 There's more detail and example code at:

 

http://www.salesforce.com/us/developer/docs/pages/index_Left.htm#CSHID=pages_dynamic_vf_maps_lists.htm|StartTopic=Content%2Fpages_dynamic_vf_maps_lists.htm|SkinName=webhelp

 

There's also a blog post of mine around this at:

 

http://bobbuzzard.blogspot.com/2011/03/visualforce-dynamic-map-bindings.html

theitdeptrockstheitdeptrocks

To the rescue again!  Thanks Bob for the code and the links.  I've been trying to solve a problem with how to determine when to render a field on a layout, based on the values of two other fields.  This should help greatly!

 

 

theitdeptrockstheitdeptrocks

Would it be possible to do something like this then?  I'm looking to be a little more dynamic with rendering fields on my layout.

 

    
    public map<string,integer> st = new map<string,integer>();
    public map<string,integer> rt = new map<string,integer>();
    public boolean getrender(string a){
    
        string x;
        string y;
        if(a !== null){
             x = rt.get(a);
             y = st.get(a);
             if(x*y==0){
                 return false;
             }
             else{
                 return true;
             }
        }

    } 

   
    

    public test3(ApexPages.StandardController controller) {
        this.Photo = (Photo__c)stdController.getRecord();
        st = new map<string,boolean>();
        rt = new map<string,boolean>();
        if(Photo.st__c == 'Service Type 1'){
            this.st.put('Collection__c',1);
            this.st.put('Location__c',0);
        }
        if(Photo.rt__c == 'Record Type 1'){
            this.rt.put('Collection__c',1);
            this.rt.put('Location__c',0);
        }
    }



<apex:inputField value="{!Photo__c.Collection__c}" rendered="{!render['Collection__c']}" />
<apex:inputField value="{!Photo__c.Location__c}" rendered="{!render['Location__c']}" />

 

theitdeptrockstheitdeptrocks

I updated the code above to what would compile, found below, but am receiving:

Error: Unknown property 'Photo__cStandardController.render'

 

public with sharing class test3 {
    public map<string,integer> st = new map<string,integer>();
    public map<string,integer> rt = new map<string,integer>();
   
    private final Photo__c p;
    public boolean getrender(string a){
    
        integer x;
        integer y;
        
             x = rt.get(a);
             y = st.get(a);
             if(x*y==0){
                 return false;
             }
             else{
                 return true;
             }
        

    } 

    public test3(ApexPages.StandardController controller) {
        this.p = (Photo__c)controller.getRecord();
        st = new map<string,integer>();
        rt = new map<string,integer>();
        if(p.Pose__c == 'Service Type 1'){
            this.st.put('Collection__c',1);
            this.st.put('Location__c',0);
        }
        if(p.Collection__c == 'Record Type 1'){
            this.rt.put('Collection__c',1);
            this.rt.put('Location__c',0);
        }
    }
}


<apex:inputField value="{!Photo__c.Collection__c}" rendered="{!render['Collection__c']}" />

 

bob_buzzardbob_buzzard

This is because your usage of render indicates to the page "compiler" that it is a map that is public with a public getter and setter.  You are attempting to pass a parameter to a getter, which is not allowed in Visualforce.