You need to sign in to do that
Don't have an account?
George M Albrecht
Set Selected Value of Custom VF Picklist to Field on Record Save
Hello,
We have built a custom picklist using SOQL on a VF page. However, we need a way to take the value that a user selected and set that to a value of a field on our object. Below is the code sample. We are trying to set the values of select list "award" and select list "denom" to two fields on our custom object.
We have built a custom picklist using SOQL on a VF page. However, we need a way to take the value that a user selected and set that to a value of a field on our object. Below is the code sample. We are trying to set the values of select list "award" and select list "denom" to two fields on our custom object.
public class employeeAwardPicklist { public string atype{get; set;} public string award{get; set;} // Added awards as public variable, removed the getter. public List<SelectOption> awards {get;set;} public string denom{get; set;} // Denomination list added for picklist public List<SelectOption> denoms {get;set;} public employeeAwardPicklist(ApexPages.StandardController controller) { } //The type of giftcard for the first picklist option public list<selectoption> gettypes(){ list<selectoption> options= new list<selectoption>(); list<AggregateResult> atype= [select Type__c from Awards__c group by type__c order by type__c]; options.add(new selectoption('', '--Select CardType--')); for(AggregateResult t:atype){ options.add(new selectoption(String.valueof(t.get('type__c')), String.valueof(t.get('type__c')))); } return options; } // Use this method to get the 2nd picklist populated. This method is called everytime the first picklist is changed. public void calculateAwards(){ awards = new List<SelectOption>(); if(atype == null || atype == ''){ ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Select a Card Type')); return; } list<Awards__c> award= [SELECT name FROM Awards__c WHERE Awards__c.type__c =:atype]; awards.add(new selectoption('—-Select Award—-', '—-Select Award—-')); for( Awards__c a:award){ awards.add(new selectoption(a.name,a.name)); } } //The denomination amount for the third picklist option. This method is called everytime that the second picklist is changed. public void calculateDenoms(){ denoms = new List<SelectOption>(); if(award == null || award == ''){ ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Select an Award')); return; } list<Awards__c> denom= [SELECT Denomination__c FROM Awards__c WHERE Awards__c.name=:award]; denoms.add(new selectoption('—-Select Denomination—-', '—-Select Denomination—-')); for( Awards__c d:denom){ denoms.add(new selectoption(d.denomination__c,d.denomination__c)); } } } <apex:pageBlockSection title="Award Details" columns="2" > <!--First picklist option Gift Card Type --> <apex:pageBlockSectionItem > <apex:outputLabel value="Gift Card Type"/> <apex:outputPanel styleClass="requiredInput" layout="block"> <apex:outputPanel styleClass="requiredInput" layout="block"/> <apex:selectList size="1" value="{!atype}" > <apex:selectOptions value="{!types}" /> <apex:actionSupport event="onchange" action="{!calculateAwards}" reRender="award"/> </apex:selectList> </apex:outputPanel> </apex:pageBlockSectionItem> <!--Second picklist option Award Name --> <apex:pageBlockSectionItem > <apex:outputLabel value="Award"/> <apex:selectList size="1" value="{!award}" id="awardList" > <apex:selectOptions value="{!awards}" /> <apex:actionSupport event="onchange" action="{!calculateDenoms}" reRender="award"/> </apex:selectList> </apex:pageBlockSectionItem> <!--Third picklist value Denomination --> <apex:pageBlockSectionItem > <apex:outputLabel value="Denomination" /> <apex:selectList size="1" value="{!denom}" id="denoms"> <apex:selectOptions value="{!denoms}"/> </apex:selectList> </apex:pageBlockSectionItem> <apex:inputField value="{!Employee_Awards__c.Award_Term__c}" /> <apex:inputField value="{!Employee_Awards__c.Award_Reason__c}" /> <apex:inputField value="{!Employee_Awards__c.Gift_Card_Serial_Number__c}" /> <apex:outputLink style="margin-left:130px" value="https://plymouthrock--SQA.cs9.my.salesforce.com/sfc/p/K000000W3om0/a/K000000002V6/0XwKI_QQteWq11TJ0JClkACGK6gUU9AqOnBRw5ABAD4=" styleClass="bold" target="_blank" id="giftCardSheet">Available Gift Cards</apex:outputLink> </apex:pageBlockSection>
That way the selected value will contain an id. However, you'll also need to do one of the following:
(1) change the SOQL query that uses the value to build the next picklist, to query on the id rather than name.
(2) Create a map of names keyed by id and query back the name for the id that the user has selected.
I'd probably go with number 1, but I don't know how straightforward that is for your code.