| 1. |
How To Get The Picklist Value In Apex Class? |
|
Answer» Using Dynamic APEX, we can achieve this. On object of type pickilist, call getDescribe(). Then call the getPicklistValues() METHOD. Iterate over result and create a list. Bind it to <apex:selectOptions>. Code Example: Let’s say we have a custom object called OfficeLocation__c. This object contains a picklist field Country__c. The first thing we NEED to do, within our controller is use the getDescribe() method to obtain information on the Country__c field: Schema.DescribeFieldResult fieldResult = OfficeLocation__c.Country__c.getDEscribe(); We know that Country__c is a picklist, so we want to RETRIEVE the picklist values: List<Schema.PicklistEntry> ple = fieldResult.gerPicklistValues(); The only thing left for us to do is map the picklist values into an <apex:selectOptions> tag can use for display. Here is the entire method from our controller to do this: public List<SelectOption> getCountries() { List<SelectOption> options = new List<SelectOption>(); Schema.DescribeFieldResult fieldResult = OfficeLocation__c.Country__c.getDescribe(); List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues(); for( Schema.PicklistEntry f : ple) { options.add(new SelectOption(f.getLabel(), f.getValue())); } return options; } With our controller logic all COMPLETE, we can call the getCountries() method from our Visualforce page, and populate the <apex:selectList> tag: <apex:selectList id="countries" value="{!Office_Location__c.Country__c}" size="1" required="true"> <apex:selectOptions value="{!countries}"/> </apex:selectList> Using Dynamic apex, we can achieve this. On object of type pickilist, call getDescribe(). Then call the getPicklistValues() method. Iterate over result and create a list. Bind it to <apex:selectOptions>. Code Example: Let’s say we have a custom object called OfficeLocation__c. This object contains a picklist field Country__c. The first thing we need to do, within our controller is use the getDescribe() method to obtain information on the Country__c field: Schema.DescribeFieldResult fieldResult = OfficeLocation__c.Country__c.getDEscribe(); We know that Country__c is a picklist, so we want to retrieve the picklist values: List<Schema.PicklistEntry> ple = fieldResult.gerPicklistValues(); The only thing left for us to do is map the picklist values into an <apex:selectOptions> tag can use for display. Here is the entire method from our controller to do this: public List<SelectOption> getCountries() { List<SelectOption> options = new List<SelectOption>(); Schema.DescribeFieldResult fieldResult = OfficeLocation__c.Country__c.getDescribe(); List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues(); for( Schema.PicklistEntry f : ple) { options.add(new SelectOption(f.getLabel(), f.getValue())); } return options; } With our controller logic all complete, we can call the getCountries() method from our Visualforce page, and populate the <apex:selectList> tag: <apex:selectList id="countries" value="{!Office_Location__c.Country__c}" size="1" required="true"> <apex:selectOptions value="{!countries}"/> </apex:selectList> |
|