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>  
  • 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 '16 at 10:17
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

| improve this answer | |

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.