6

I am trying to render one component based on whether another one has a specific value set on a Visualforce page.

Using the following, it works fine EXCEPT on the initial load (i.e. once I start toggling the driving question, the dependent question starts hiding/showing fine).

<apex:pageBlockSection id="myId">
    <apex:inputField value="{!My_Object__c.Question_1__c}">
        <apex:actionSupport event="onchange" reRender="myId"/>
    </apex:inputField> 
    <apex:inputField value="{!My_Object__c.Dependent_Question__c}" rendered="{!My_Object__c.Question_1__c == 'Yes'}">
</apex:pageBlockSection>

"Question_1__c" is a pick-list with the values Yes and No, defaulting to Yes.

I'm wondering if there is something going on where on the initial load the value is blank until I actually select the value (or save the page).

EDIT: based on discussion below, it looks like the field is initially set to NULL. However, checking for NULL as part of the rendered expression would also mean the field would show up when the picklist it is dependent on is set to the empty "--None--" value. The question becomes whether there is a way to differentiate the NULL of the initial page load vs the NULL being set manually by a user purely in Visualforce, or if a custom controller would be require to initialize the field (which would for all intents and purposes override the "default" value set in the configuration of the field).

5

Boolean fields have three states, true, false, and null. It sounds like your picklist might be exhibiting the same issue.

Try prepopulating your object during the construction or action phase of the controller.

ie:

    public Class MyController{
         public object__c myObject__c {get;set;}
         public MyController(){
             myOjbect__c= new object__c(Question_1__c = 'Yes');
        }
    }
  • The Visualforce page that I am constructing doesn't currently have a custom controller (primarily just displaying a bunch of fields dynamically based on other fields), so I was hoping to find a solution that didn't require a custom controller (though I suspect your answer is valid) – Michael Welburn Sep 10 '12 at 16:40
  • @ebt Thought I would put a mention here that I based my answer (after many edits) On yours, you are correct that the object field is a null value rather than having the default value – Jon Hazan Sep 10 '12 at 16:58
3

Edit: I see you mean to only have the specific sub question change. After extended comment discussion and also mention to @ebt the problem is that when not passed an id for the object the object is being created with NULL values rather than the defaults.

EDIT 2 Based on the edit in the question you should add a check to see if the Object ID is null as well as the field value.

Try changing your rendered statement to:

<apex:inputField value="{!My_Object__c.Dependent_Question__c}" rendered="{!My_Object__c.Question_1__c == 'Yes' || (ISNULL(My_Object__c.Question_1__c) && ISNULL(My_Object__c.Id))}">

I have tested this using the Case object as below:

<apex:page StandardController="Case">
<apex:form>
<apex:pageBlock>
<apex:pageBlockSection id="myId">
    <apex:inputField value="{!Case.Status}">
        <apex:actionSupport event="onchange" reRender="myId"/>
    </apex:inputField> 
    <apex:inputField value="{!Case.Origin}" rendered="{!Case.Status=='New' || (ISNULL(Case.Status) && ISNULL(Case.Id))}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
  • Wouldn't that "rendered" formula essentially be the same thing? It wouldn't let me save it returning 1 or 0 as the result, so I tried switching it to TRUE or FALSE (which ends up being redundant) – Michael Welburn Sep 10 '12 at 16:39
  • Hi Michael, I've updated the answer with the full code I used to get this to work for me :) As for the rendered formula being the same I am unsure and someone can probably correct me but I've always used an IF to return the true or false values. – Jon Hazan Sep 10 '12 at 16:40
  • 1
    Case.Status == 'New' will return a true/false boolean by itself -- the actual logic behind the IF() statement is it evaluates the first parameter to true/false, then returns the second parameter if true or the third parameter if false. – Michael Welburn Sep 10 '12 at 16:43
  • I guess I'm not understanding the problem correctly. Have you got some more info on the setup of the My_Object__c as the code above seems to show hide correctly for me including the initial load – Jon Hazan Sep 10 '12 at 16:47
  • Perhaps there is something on a custom object that doesn't work right (as opposed to a standard one). What you provided above seems like it logically should work (as should the problem code I posted), but there must be some odd piece of logic that is stopping it. – Michael Welburn Sep 10 '12 at 16:48

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.