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
SFDC TechSFDC Tech 

Help with a picklist in a visual force page

Hi,

I am new to visual force. and i am looking for help with the following task.

I have the following code for a visual force page which has two picklists.

<apex:page standardController="Quote" extensions="SelectionExtension" showHeader="true" sidebar="true">
    <apex:form >
    <apex:pageMessages />
    <apex:pageBlock title="Select Billing Type and Price Group">
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" rendered="{!quo!=null}" action="{!save}"/>
            <apex:commandButton value="Cancel" action="{!cancel}"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection >
            <apex:selectList value="{!billingCode}" label="Billing Type" size="1">
                <apex:selectOptions value="{!availableBillingTypes}"/> 
                <apex:actionsupport event="onchange" rerender="priceBookList" />
            </apex:selectList>
            <apex:selectList value="{!priceGroup}" label="Price Group" Id="priceBookList" size="1" disabled="{!availablePriceGroups.size=0}">
                <apex:selectOptions value="{!availablePriceGroups}"/> 
            </apex:selectList>
        </apex:pageBlockSection>        
    </apex:pageBlock>
    </apex:form>
</apex:page>


The picklist values Billing Type and Price Group(dependent picklist) is retrieved from the following class through dyanamic soql

public with sharing class PriceGroupSelectionExtension {
    public Quote quo{get;set;}
    Map<String, List<Billing_Client_Type_Price_Group__c>> billingTypeToPriceGroupsMap {
        get{
            if(billingTypeToPriceGroupsMap==null){
                List<Billing_Client_Type_Price_Group__c> billingTypes= [SELECT Billing_Client_Type_Code__c, Billing_Price_Group_Code__c, Billing_Price_Group_Code__r.Name from Billing_Client_Type_Price_Group__c
                                                                        where Billing_Client_Type_Code__c!=null and Billing_Price_Group_Code__r.Hide_In_ICE__c=false];
                billingTypeToPriceGroupsMap= GroupBy.strings('Billing_Client_Type_Code__c', billingTypes);
            }
            return billingTypeToPriceGroupsMap;
        }
        set;
    }

    public SelectionExtension(ApexPages.StandardController controller) {
        Id quoteId = (Id) ApexPages.currentPage().getParameters().get('Id');
        if( quoteId==null){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'You must access this page from a quote'));
        } else {
            this.quo=(Quote) controller.getRecord();
        }

        if(illegalURLs()){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'The website link provided leads to an unexpected page, please close this page'));
        }
        billingCode=availableBillingTypes.isEmpty()?null:availableBillingTypes[0].getValue();
    }

    private Boolean illegalURLs(){
        if((ApexPages.currentPage().getParameters().get('retURL')!= null && 
                !ApexPages.currentPage().getParameters().get('retURL').startsWith('/'))
            ||(ApexPages.currentPage().getParameters().get('cancelURL')!= null && 
                !ApexPages.currentPage().getParameters().get('cancelURL').startsWith('/'))
            ||(ApexPages.currentPage().getParameters().get('saveURL')!= null && 
                !ApexPages.currentPage().getParameters().get('saveURL').startsWith('/'))){
            return true;
        }
        return false;
    }

    public String billingCode{
        get;
        set{
            billingCode=value;
            refreshAvailablePriceGroups();
        }
    }

    public String priceGroup{
        get;
        set{
            priceGroup=value;
            if(billingTypeToPriceGroupsMap.containsKey(billingCode)){
            quo.Price_Group__c = value;
            quo.Solution__c = GroupBy.ids('Billing_Price_Group_Code__c', billingTypeToPriceGroupsMap.get(billingCode)).get(value)[0].Id;    
        }
        }
    }

    public List<SelectOption> availablePriceGroups{get;private set;}

    public List<SelectOption> availableBillingTypes{
        get{
            if(availableBillingTypes==null){
                availableBillingTypes= new List<SelectOption>();
                for(String billingTypeCode: billingTypeToPriceGroupsMap.keySet()){
                    availableBillingTypes.add(new SelectOption(billingTypeCode, billingTypeCode));          
                }
            }
            return availableBillingTypes;
        }
        private set;
    }

    private void refreshAvailablePriceGroups(){
        List<SelectOption> priceGroups= new List<SelectOption>();
        if(billingTypeToPriceGroupsMap.containsKey(billingCode)){
            for(Billing_Client_Type_Price_Group__c billingTypePriceGroup: billingTypeToPriceGroupsMap.get(billingCode)){
                if(billingTypePriceGroup.Billing_Price_Group_Code__c!=null && billingTypePriceGroup.Billing_Price_Group_Code__r.Name!=null){
                    priceGroups.add(new SelectOption(billingTypePriceGroup.Billing_Price_Group_Code__c, billingTypePriceGroup.Billing_Price_Group_Code__r.Name));
                }           
            }
        }
        availablePriceGroups=priceGroups;
    }
    
    public PageReference cancel(){
        if(ApexPages.currentPage().getParameters().get('cancelURL') !=null){
            return new PageReference(ApexPages.currentPage().getParameters().get('cancelURL'));
        } else{
            return new PageReference(ApexPages.currentPage().getParameters().get('retURL'));
        }
    }
    
    I would like to know how to set up a particular picklist value as default in both the picklists.
and also would like to know how to sort these values based on ranking.

Please advise!!
SFDC TechSFDC Tech
Hi friends,

Happy new year!
Let me know if additional info is needed on this.
Look forward for a reply.

Thanks in advance!
pconpcon
To set a default value I would just have a class level variable that is whatever you want your default to be.  Then in the apex:selectList just set value to be your class level variable and that will be your default (it looks like you are trying to do that, but you are doing it in the set [with a variable? called value that you do not define anywhere]).  As for sorting you will need to either sort them in your SOQL or create a custom iterator [1] that you sort and use to generate your select list.

[1] http://blog.deadlypenguin.com/blog/2015/10/10/comparable-sorting-objects-in-salesforce/
SFDC TechSFDC Tech
hello there,
lets assume the picklist values in the VF page for 
field 1 is A,BC,D and E field 2 pick list values is dependent on field 1 thru dynamic VF class

could you please help in pointing me at the code to
do the following
1)Set the default value in field 1
2)Sort field 1 and 2 based on ranking.

Since i am not a VF expert any additional tips in modfying the current code(pointing at the correct place where changed need to happen) is appreciated.

 
pconpcon
To set the default value for field one you would just need to set your billingCode variable to whatever the default should be.  You can do this as part of the constructor, you can do this at the time when you retrieve your list of value, there are lots of places when you can do this how you do this.  It's all dependent on your needs / structure.

To sort the fields you'll want to take a look at the link I provided and implement your own Comparable list (assuming you cannot sort it how you want in the SOQL.  Then create a List of the Comparable objects and call .sort on it.

I'm not going to modify your existing code because I don't know all of your business rules for how the sorting should be done or where the default values come from.  Additionally it makes it harder for you to maintain if you do not understand why and how it was put together.  If you make an attempt to modify your code using the information here and run into any specific problems, I will be glad to take a look at the code you have generated to help you.

You may also find this article [1] useful.

NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.

[1] http://blog.deadlypenguin.com/blog/2012/07/09/dynamic-dependent-picklists-in-salesforce/