0

How can I display a lookup field based on a value I selected from a picklist field?

Assuming I have a custom object named Expense__c. This object contains a picklist field named Type__c.

This field contains two values: Utilities and Payroll.

A lookup field (Related to Employee table) must be displayed upon selecting Payroll.

This field must be exclusive only to that value, meaning upon selecting Utilities, this field would disappear.

I think using rerender makes it possible, but I don't know how?

3 Answers 3

0

A simple example (no controller extension required) using apex:actionSupport that detects the change event on the selectList and rerenders an always visible component (the outputPanel). Within the outputPanel is the inputField with the rendered= condition.

<apex:selectList value="{!Expense__c.Type__c}">
   <apex:actionSupport event="onchange" rerender="lookupOP"/>
   <apex:selectOption itemvalue="Payroll" itemLabel="Payroll"/>
   <apex:selectOption itemvalue="Utilities" itemLabel="Utilities"/>
</apex:selectList>

<apex:outputPanel id="lookupOP">
  <apex:inputField value="{!Expense__c.LookupField__c}" 
                   rendered="{!Expense__c.Type__c = 'Payroll'}"/>
</apex:outputPanel>
0
0

You need to have a flag (boolean property) to set/unset upon the value you select from the picklist.

Then in the lookup field pass your flag to rendered attribute.

0

What you need to do is use Action support on your input field. On change of your input field will fire the controller method which will populate the lookup flied querying the data from database.

<apex:page controller="MyCustomController">
  <apex:form id="myForm">
  <apex:pageBlock title="Sample"> 
      <apex:pageBlockSection>
          <apex:inputField value="{!acc.type}" >
              <apex:actionSupport action="{!populateParentAccount}" event="onchange" reRender="myForm"/>                  
          </apex:inputField>
          <apex:inputField value="{!acc.ParentID}"/>
     </apex:pageBlockSection>
  </apex:pageBlock>      
  </apex:form>
</apex:page>

Controller code is simple.

public class MyCustomController{

public Account acc{get;set;}
public MyCustomController(){
acc=new Account();

}

public void saveAccount(){
    upsert acc;
}

public void populateParentAccount(){
    if(this.acc.Type=='Other') 
        acc.ParentID=[Select id from account where name='ACC'][0].id;
}
}

The magic here happens at action support which on change of type calls populateParentAccount which populates the value of parentID in account instance. Now comes the role of rerender tag, which reflects the newly populated value onto page.

You must log in to answer this question.

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