function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ritika JRitika J 

Getting picklist values based on user input



I have a requirement in which i have to make a dynamic picklist , such that picklist values should change as per the value selected by user in other field.


I have two fields one is look up and another is picklist , Whatever user selects in look up field   i have to make a query depending on that input and show picklist values.


My code


<apex:page standardController="Account" extensions="Accountextension" >
<apex:form >
<apex:pageBlock id="thePageBlock" >
<apex:pageBlockSection >
<apex:inputField id="product" value="{!Account.Product__c}">
<apex:actionSupport event="onchange" rerender="thePageBlock"
<apex:pageBlockSectionItem >
<apex:outputLabel value="Products available" for="Productsavailable"></apex:outputLabel>
<apex:selectList id="Productsavailable" size="1" title="Products available">
<apex:selectOptions value="{!mgrs}"></apex:selectOptions>



public class Accountextension {
    private final Account acc;
    String product;
    public String getProduct() {
    return acc.Product__c ;
    public void setProduct() {

        public Accountextension(ApexPages.StandardController stdController) {
        this.acc = (Account)stdController.getRecord();
            //builds a picklist
          public List<selectOption> getMgrs() {
           System.debug ('**********************LOGIN1*********************'+acc.Product__c);

        List<selectOption> options = new List<selectOption>(); //new list for holding all of the picklist options
        options.add(new selectOption('', '- None -')); //add the first option of '- None -' in case the user doesn't want to select a value or in case no values are returned from query below
        for (Product2 prod : [Select id ,Name From Product2 where id = :product]) { //query for products
                   options.add(new selectOption( ,String.valueOf(prod.Name))); //for all records found - add them to the picklist options
        return options; //return the picklist options



My problem is  in the query (highlighted in bold)  it is not  recognising the id in query , though i am getting the correct id  .


Debug log:


22:51:50.054 (54454000)|SYSTEM_METHOD_EXIT|[20]|System.debug(ANY)
22:51:50.054 (54482000)|SYSTEM_METHOD_ENTRY|[23]|LIST.add(ANY)
22:51:50.054 (54513000)|SYSTEM_METHOD_EXIT|[23]|LIST.add(ANY)
22:51:50.054 (54530000)|SOQL_EXECUTE_BEGIN|[24]|Aggregations:0|Select id ,Name From Product2 where id= :product
22:51:50.057 (57518000)|SOQL_EXECUTE_END|[24]|Rows:0

Please  help me with this problem





    something is unusual in the apex code.You can assign acc.Product__c in the string variable instead of using the getter and setter property.


 String Product = string.valueof(acc.Product__c);


After this you can use the variable in the SOQL  Query.


Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.