0

Let's say I have an object named Device__c. This has a picklist field named Type__c, which has two values: Laptop and Mobile.

Upon selecting Laptop, a picklist named Brand__c must be displayed. Brand__c picklist field contains two values: Apple and Acer.

Upon selecting Apple, a lookup field named Models__c must be displayed.

My question is, how can I filter this lookup field with such controlling picklist fields using Apex and visualforce?

By the way, here's my code:

vf page

<apex:page standardController="Device__c">
    <apex:form >
        <apex:pageblock >


            <apex:selectList label="Device Type:" value="{!Device__c.Type__c}" size="1">  

                 <apex:actionSupport event="onchange" reRender="b1" status="status"/>              
                 <apex:selectOption itemValue="--None--" itemLabel="--None--"/> 
                 <apex:selectOption itemValue="Laptop" itemLabel="Laptop"/> 
                 <apex:selectOption itemValue="Mobile" itemLabel="Mobile"/> 
            </apex:selectList> 
            <apex:actionStatus startText="applying value..." id="status"/> <br/>



            <apex:pageBlockSection id="b1"> 
                <apex:selectList label="Brand" value="{!Device__c.Brand__c}" size="1">
                <apex:selectOption itemValue="Apple" itemLabel="Apple"/> 
                 <apex:selectOption itemValue="Acer" itemLabel="Acer"/>             
                </apex:selectList>
                <apex:inputField value="{!Device__c.Models__c}" rendered="{!Device__c.Type__c='Laptop'}"/> 
                <apex:inputField value="{!Device__c.Device_Brand__c}" rendered="{!Device__c.Type__c='Laptop'}"/> 

            </apex:pageBlockSection> 

        </apex:pageblock>
    </apex:form>  
</apex:page>  
1
  • As Type__c and Brand__c are picklists, you can directly put the options in object definition. You don't need to put those in VF page. And what is the field Device_Brand__c stands for?
    – Saroj Bera
    Jun 13, 2016 at 10:17

1 Answer 1

2

You can specify lookup filter criteria.

Let's assume you have object Device and Model

Model has text field brand and name

Device has picklist brand and lookup to model

Click on your lookup field, click edit and proceed to Lookup filter section

                    field           operator    value
Filter Criteria     model:  Brand   equals      Device: Brand
Filter Type Required. The user-entered value must match filter criteria.

Now when you selecting brand picklist, lookup will be filtered:

enter image description here

You must log in to answer this question.

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