[SalesForce] Lookup Filter Based on Selected Picklist Value Using Apex and Visualforce

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>  

Best Answer

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