2

How to display dependent pick-list fields and their values using dynamic apex in visualforce page.

I need to have multiselect hence i am using the below code -

public List<SelectOption> getDept()
    {
        List<SelectOption> options = new List<SelectOption>();
        Schema.DescribeFieldResult departfield =  Activity_Tracker__c.Category__c.getDescribe();
        List<Schema.PicklistEntry> picklist = departfield.getPicklistValues();
        for( Schema.PicklistEntry f : picklist){      
        options.add(new SelectOption(f.getLabel(), f.getValue()));
        }    
        return options;
    }



public List<SelectOption> getCustb()
    {
        List<SelectOption> options = new List<SelectOption>();
        Schema.DescribeFieldResult departfield =  Activity_Tracker__c.SubCust__c.getDescribe();
        List<Schema.PicklistEntry> picklist = departfield.getPicklistValues();
        for( Schema.PicklistEntry f : picklist){      
        options.add(new SelectOption(f.getLabel(), f.getValue()));
        }    
        return options;
    }

Can u please check how to make it dependent.

  • Have you already tried or looked up how to get started on this ? Please understand that short and undetailed questions don't really give us much info on what you really want to know that's not already covered in the many other questions around this subject (on this site and elsewhere) – Samuel De Rycke May 7 '15 at 12:25
  • Hi I have updated my question – user2542953 May 7 '15 at 12:28
  • This may be useful to you: salesforce.stackexchange.com/a/1252/151 – JCD May 7 '15 at 13:50
  • also pls add the VF page if you still require an answer. – Guy Clairbois May 7 '15 at 14:55
0

If you have defined field dependency in the backend, you can get it in page without much coding effort by simply using . Please check below link, https://www.salesforce.com/docs/developer/pages/Content/pages_quick_start_dependent_picklists.htm

0

I am assuming you are not using the out-of-the-box dependent picklist here. You can modify your existing code like this.

public String selectedValue{get;set;}

public List<SelectOption> getCustb()
    {
        List<SelectOption> options = new List<SelectOption>();
        Schema.DescribeFieldResult departfield =  Activity_Tracker__c.SubCust__c.getDescribe();
        List<Schema.PicklistEntry> picklist = departfield.getPicklistValues();
        for( Schema.PicklistEntry f : picklist){ 
        switch(selectedValue)
        {
         case 'Fruit': {if(f.getValue()=='Mango')
                 options.add(new SelectOption(f.getLabel(), f.getValue())); 
                       }
        case 'Cars': {if(f.getValue()=='Toyota')
                 options.add(new SelectOption(f.getLabel(), f.getValue())); 
                       }
        }

        }    
        return options;
    }

Visualforce:

<apex:selectList value={!selectedValue} size="1" rerender="dependant">
<apex:selectOptions value={!Dept}/></apex:selectList>

<apex:selectList id="dependant" value={!selectedValue} size="3">
<apex:selectOptions value={!Custb}/></apex:selectList>

This trick will work for small picklists where its possible to hardcode. If its a large list, you can prefix a number in all the options to map them with the master picklist. Example: 1Fruit -- 1Mango, 1Apple then 2Cars -- 2Toyota, 2Audi and filter using the prefix number inside the above for loop.

All said and done, I would suggest you to go for Dependent Picklist here, with almost zero code, its much simple, easy and reliable.

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.