+ Start a Discussion
willardwillard 

Defaulting a value in a SelectList when you don't know the values beforehand

I've already seen the examples where you can set "CANADA" to be the default country when the selectListOptions are a known quantity.

 

However, I have to generate a selectList dynamically, and then based on the result set, choose an appropriate default.

 

I have a business_unit field.  Once I pick a business_unit, I need to populate a BOSS Unit field.  However, the default BOSS Unit may be different for each publication.  Here's what I have:

            <apex:pageBlockSectionItem >
                <apex:outputLabel value="BOSS Unit" for="bossUnit"/>
                <apex:selectList value="{!defBossUnit}" id="bossUnit" size="1">
                    <apex:selectOptions value="{!BOSSUnits}"/>
                </apex:selectList>
            </apex:pageBlockSectionItem>

 

Here's my controller code.  Basically some business_units have the default as PG, others as PAGE:

    public List<SelectOption> getBOSSUnits() {
        List<BossUnitSize__c > units = [Select b.Name, b.UnitSize__c from BossUnitSize__c b where b.SetID__c = :pricing.business_unit__c
                    order by b.Name];
        List<SelectOption> options = new List<SelectOption>();
        for (BossUnitSize__c unit : units) {
            options.add(new SelectOption(unit.unitSize__c, unit.Name));
            if (unit.Name == 'PAGE' || unit.Name == 'PG') {
                defBossUnit = unit.Name;
            }
        }
        
        return options;
    }

 The above code does NOT default the defBossUnit correctly.  Any ideas?

willardwillard

arrgh - stupid mistake.  I was placing the wrong value into the field, which wouldn't update the page.  It should have been:

 if (unit.unitSize__c == 'PAGE' || unit.unitSize__c== 'PG') {
                defBossUnit = unit.unitSize__c;
            }