2

I would like to do this by replicating the Contact page in Visualforce and then overriding the original Contact layout with the Visualforce page, which would include the code to make hiding/showing the text field based on the correct multi-picklist option being selected, possible.

I'd make use of Record Types, but that would mean the user having to save to make the text field appear. I'm interested in making the text field appear as soon as the user selects the option, "Other", on the multi picklist.

Here's what I have so far (thanks to @Ashwani):

<apex:page standardController="Contact">

   <apex:form >

      <apex:detail />

      <apex:inputfield value="{!multi-picklist}">

          <apex:actionSupport event="onchange" reRender="Form" />
     </apex:inputfield>

     <apex:outputpanel id="Form">
        <apex:inputtext value="{! TextField}" rendered="{!multi-picklist == 'Other'}" />
     </apex:outputpanel>
  </apex:form>

</apex:page>

The problem is that while this does cause the multi-picklist to appear on the screen, "Other" (the multi-picklist value which should control the visibility of the text field) is absent from the multi-picklist.

I'm going to keep working to find out what's wrong, and will post an answer if I do figure it out myself. In the meantime, if anyone can see any obvious mistakes, please post an answer or comment.

  • 1
    If you're new to visualforce, try running through the Visualforce Trailhead. Should help you get a solid grasp on the basic principles before you start working on your own projects. – battery.cord Sep 23 '16 at 13:13
2

There are some basic errors in visualforce. Your page should look like this:

<apex:page standardController="Contact">

   <apex:form >

      <apex:detail />
      <!-- bind correct value here. It must be sObject -->
      <apex:inputfield value="{!multi-picklist}">
          <!-- on change event -->
          <apex:actionSupport event="onchange" reRender="panelID" />
     </apex:inputfield>

     <!-- This panel will be re-rendered on change -->
     <apex:outputpanel id="panelID">
        <apex:inputtext value="{! TextField}" rendered="{!multi-picklist == 'Other'}" />
     </apex:outputpanel>
  </apex:form>

</apex:page>


Few things:-
- <actionsupport/> tag event attribute was wrong.
- rerender attribute must be Id of element you want to refresh/rerender it can not be entire tag name.
- It is good rerender parent element instead of exact element because exact element may not be available based on rerender condition.
- In inputfield tag rerender and value attribute value was interchanged.
- inputfield can only bind with Sobject class instance.

  • 1
    Assuming that the value="{!multi-picklist}" is properly bound, does changing the value in an <apex:inputField> cause the value of that field to be updated in the controller's sObject instance (without the need to save the record)? I think that would need to be the case if the re-render were to work properly. Otherwise, js remoting might be required. – Derek F Sep 23 '16 at 13:56
  • This results in an error: The element type "apex: inputfield" must be terminated by the matching end-tag "</apex: inputfield>". Thank you for the "Few Things" though, I will look into them. – user34868 Sep 23 '16 at 16:04
  • 1
    @ESiL this happens when a tag is not closed properly. I suggest you to go through visual force basic given in comment of question – Ashwani Sep 23 '16 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy