I have the function which returns the boolean value and I want to use it in the VisualForce page in the following way:

<apex:inputField id="reason" value="{!if(!isReason()): Case.Reason__c = 'No Reason' | Case.Reason__c)}" required="true"/>

How can I achieve this?


I would tend to say that you need to set the inputfield value to your custom field "Case.Reason__c", this way it can also be updated manually as you have set it to required.

Then in your constructor set the value based on your logic, or if needed based on an action in the page use an actionfunction to perform the same and actually set it, rerendering it afterwards.

  • This field has all values set in the Case Fields. So, I just need to get the value of the function and leave this field unset (the user will choose a value) or choose the value in this input field.
    – heisenberg
    Jan 17 '19 at 13:21
  • 1
    @heisenberg This answer is a perfectly valid approach: you'd simply set the Reason__c in your controller/extension logic during construction. This sets a default value that the user can change later. This is necessary because inputField can't work on expressions, only actual field references.
    – sfdcfox
    Jan 17 '19 at 14:01
  • @heisenberg I suppose your "Reason__c" field is some kind of picklist, meaning that if you set the inputfield like following (assuming "caseVariable" is your case variable in your controller): value="{!caseVariable.Reason__c}" You shall be able to set it from the controller, either in the constructor or in an actionfunction.
    – Péqueño
    Jan 17 '19 at 14:13

You may need to use addFields to access Reason__c field explicitly inside constructor. You can build logic in constructor like:

public Case cs {get; set;}
public StackExchangeController(ApexPages.StandardController stdController){
    stdController.addFields(new String[]{ 'Reason__c' });
    cs = (Case)stdController.getRecord();
    cs.Reason = !isReason() ? 'No Reason' : cs.Reason__c;

Note: Use '' instead of 'No Reason' if you want to unset.

Your visualforce code look like:

<apex:inputField id="reason" value="{!cs.Reason__c}" required="true"/>

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.