[SalesForce] How to assign Custom Picklist value to a Custom field

I want to display a custom picklist value in the Custom field whose datatype is Decimal.

I have written some code to show custom Picklist and able to select the decimal value. But, I want to assign this value to a Custom Field Specific_Discount__c (Datatype : Decimal).

Apex:

public class PriceGroupConfigControllerExt {

    public final MyObject_Config__c myObj {get; private set;} 
    ApexPages.StandardController stdController;
    public list<SelectOption> pickList {get; private set;}
    public String selectedId {get; set;}

public PriceGroupConfigControllerExt(ApexPages.StandardController Controller) {  

        stdController = Controller;                
        this.myObj = (MyObject_Config__c)stdController.getRecord(); 

    List<MyObject__c> mpgList = [Select Id, MPG_Code__c,Price__c                                
                                 From MyObject__c 
                                 Where MPG_Code__c =: myObj.MPG_Code__c 
                                ];  

    if (pickList == null) {
            pickList = new list<SelectOption>();
        }

        SelectOption firstOption = new SelectOption('', ' - Select Discount price' + ' -');
        pickList.add(firstOption);
        for MyObject__c mpgObj : mpgList) {
                this.pickList.add(new SelectOption(mpgObj.id,string.valueOf(mpgObj.Price__c)));
        }
}

public PageReference save() {
        myObj.MPG_Code__c = mpgcObj.MPG_Code__c;
        myObj.Specific_Discount__c = ?????

        try {
            update myObj ;
        } catch (Exception ex) {
                    ApexPages.addMessage(new ApexPages.Message(
                                ApexPages.Severity.ERROR,
                                ex.getMessage()));                     
        }       

        return null;

    }

Visualforce Page

<apex:selectList id="discountPrice" value="{!selectedId}" size="1" >
   <apex:selectOptions value="{!pickList}"/>
</apex:selectList> 

Best Answer

Iterate through the picklist options, find the selected and cast the value (which is the label of the picklist in your case):

for (SelectOption selOption : pickList)
{
    if (selOption.getValue() == selectedId)
    {
        myObj.Specific_Discount__c = Decimal.valueOf(selOption.getLabel());
    }
}
Related Topic