3

Is it possible to call an Apex method (not a getter) from a render check? I have tried multiple ways but get errors such as "Unknown method: readOnlyField()". I've included a stripped down version of the code I'm working with. I'm willing to use outputPanels around the output/input fields but they rely on render checks so I doubt that will help.

I would like to avoid using JS to lock down the fields afterwards due to other complications on the page.

Apex:

public Set<String> readOnlyFields = new Set<String>{'ReadOnlyField1'};
public Boolean readOnlyField(String checkString){
    return readOnlyFields.contains(checkString);
}

VF:

<apex:pageBlockSection title="Dynamic List" >
    <apex:repeat value="{!dynamicFields}" var="f">
        <apex:inputField value="{!utilityDetails[f]}" rendered="{!IF(readOnlyField(f), false, true)}" />
        <apex:outputField value="{!utilityDetails[f]}" rendered="{!IF(readOnlyField(f), true, false)}" />
    </apex:repeat>
</apex:pageBlockSection>  

2 Answers 2

5

You can't provide parameters to a function that way (readOnlyField(f)), so you would indeed need getters. The closest approximation would be a map:

public Map<String, Boolean> readOnlyField { get; set; }
// ... //
readOnlyField = new Map<String, Boolean> { 'Name' => false /* , ... */ };

Since missing keys will cause a crash, you'll have to make sure your map is fully populated.

Edit:

As an alternative, make a wrapper class, and use that instead:

public class Field {
    public String fieldName { get; set; }
    public Boolean readOnly { get; set; }
}
public Field[] fieldList { get; set; }

Then, you can render a list of Fields:

<apex:repeat value="{!fieldList}" var="field">
    <apex:inputField rendered="{!not field.readOnly}" value="{!record[field.fieldname]}" />
    <apex:outputField rendered="{!field.readOnly}" value="{!record[field.fieldName]}" />
</apex:repeat>
1
  • I like this idea. I'll try it out. I'll populate the new map as I load up the field list to make sure it is fully populated, as you suggested. Feb 20, 2015 at 19:26
1

Salesforce doesn't support passing method arguments to methods from VF expressions.

In this case, you will have to create a wrapper class with field name, and mutability and use those values in the expressions.

Apex Wrapper Class:

public class FieldInfo {
    public String fieldName {get;set;}
    public Boolean readOnly {get;set;}
}

Controller:

public Set<FieldInfo> dynamicFields {get;set;} //construct dynamic fields with readonly visibility

VF:

<apex:pageBlockSection title="Dynamic List" >
    <apex:repeat value="{!dynamicFields}" var="f">
        <apex:inputField value="{!utilityDetails[f.fieldName]}" rendered="{!Not(f.readOnly)}" />
        <apex:outputField value="{!utilityDetails[f.fieldName]}" rendered="{!f.readOnly}" />
    </apex:repeat>
</apex:pageBlockSection>  

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .