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
George M AlbrechtGeorge 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. 
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>
bob_buzzardbob_buzzard
The selected values will be in the controller properties award and denom, so you can just assign these values to your fields in the onchange handlers if you want. If the record is being managed by the standard controller, you'll need a way to get at that.  For example:


private ApexPages.StandardController stdCtrl {get; set;}

public employeeAwardPicklist(ApexPages.StandardController controller) 
{ 
   stdCtrl=controller;
}


...

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));

    Employee_Awards__c rec=stdCtrl.getRecord();
    rec.Award__c = award; // replace Award__c with whatever your field name is.
}


George M AlbrechtGeorge M Albrecht
Thanks for the help Bob, this is a step in the right direction. Issue I'm experiencing now though referencing your suggestion is that  my "Award__c" field that I'm trying to set the value for is a lookupfield to a seperate Award object, so I'm receiving an Illegal assignment error. Any suggestions on logic to get the Id or get around this?
bob_buzzardbob_buzzard
You need to change your selectoption so that the id is the value and the name is the label, e.g.

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.id,a.name));
}

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.




George M AlbrechtGeorge M Albrecht
Updated and still receiving an Illegal assignment from SObject to SOBJECT:Employee_Awards__c error Line 37. I opted for option one you have suggested in the below code sample: 
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;}
    private ApexPages.StandardController stdCtrl {get;set;}
    public employeeAwardPicklist(ApexPages.StandardController controller) {
    	stdCtrl=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 Order by name];
   awards.add(new selectoption('—-Select Award—-', '—-Select Award—-'));
   for( Awards__c a:award){
     awards.add(new selectoption(a.id,a.name));
   }
    Employee_Awards__c rec=stdCtrl.getRecord();
    rec.Awards__c = award;
}

bob_buzzardbob_buzzard
My bad - extracting the record from the standard controller requires a cast to the correct type:

Employee_Awards__c rec=(Employee_Awards__c) stdCtrl.getRecord();


George M AlbrechtGeorge M Albrecht
Thanks for the help bob, between this most recent suggestion and a few tweaks to the VF page I was able to get this working, much appreciated.