You need to sign in to do that
Don't have an account?
bohemianguy100
remove value from picklist
I have a selectList and I'd like to remove one of the values from the selectList as it should no longer be an option for new records, but I need to keep it for historical data so I don't want to delete the value.
Here is my apex code:
public List<SelectOption> getEngagementTypes() { List<SelectOption> options = new List<SelectOption>(); Schema.DescribeFieldResult rsEngagementType = Schema.sObjectType.Pricing__c.fields.EngagementType__c.getSObjectField().getDescribe(); for(Picklistentry entry : rsEngagementType.getpicklistValues()) { options .add(new SelectOption(entry.getValue(), entry.getLabel())); } return options; }
Here is my visualforce page code for the selectList:
<apex:selectList value="{!Pricing__c.EngagementType__c}" rendered="{!isEditPage}" size="1" multiselect="false" styleClass="ddlEngagementType"> <apex:selectOptions value="{!EngagementTypes}"/> <apex:actionSupport action="{!engagementChangeSave}" event="onchange" reRender="SaveMsg,PricingBaseServices" status="SaveStatus"/> </apex:selectList>
How can I remove a specific value from the selectList?
Thanks for any help.
First thing I would recommend is storing the options in a private class variable which is initialized in the class constructor or an initialization block and having the getter method (getEngagementTypes) simply return the class variable. This would be more efficient in that the database would be hit only one time during class initialization.
From the documentation found here: Describe Field Result Methods we see that there are a few methods for the PickListEntry Object
In your for loop, you can check if the entry is the one you don't want.
if(entry.getLabel() != 'disabledValue') options .add(new SelectOption(entry.getValue(), entry.getLabel()));
Cheers,
Tyler
All Answers
First thing I would recommend is storing the options in a private class variable which is initialized in the class constructor or an initialization block and having the getter method (getEngagementTypes) simply return the class variable. This would be more efficient in that the database would be hit only one time during class initialization.
From the documentation found here: Describe Field Result Methods we see that there are a few methods for the PickListEntry Object
In your for loop, you can check if the entry is the one you don't want.
if(entry.getLabel() != 'disabledValue') options .add(new SelectOption(entry.getValue(), entry.getLabel()));
Cheers,
Tyler
What I also found interesting about this was that in (standard objects > picklist field ) one can set or clear the active flag for picklist entries. However in custom objects the active flag is not available. If the active flag were available, I would uncheck it in the setup screen then use Picklistentry.isActive() in my code to determine whether or not to show a particular entry value and not hard code the value of the disabled entry.
Tyler
Thank you! I appreciate the help.