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
bohemianguy100bohemianguy100 

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.

Best Answer chosen by Admin (Salesforce Developers) 
MrTheTylerMrTheTyler

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

 

 

getLabelStringReturns the display name of this item in the picklist
getValueStringReturns the value of this item in the picklist
isActiveBooleanReturns true if this item must be displayed in the drop-down list for the picklist field in the user interface, false otherwise
isDefaultValueBooleanReturns true if this item is the default value for the picklist, false otherwise. Only one item in a picklist can be designated as the default.

 

 

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

MrTheTylerMrTheTyler

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

 

 

getLabelStringReturns the display name of this item in the picklist
getValueStringReturns the value of this item in the picklist
isActiveBooleanReturns true if this item must be displayed in the drop-down list for the picklist field in the user interface, false otherwise
isDefaultValueBooleanReturns true if this item is the default value for the picklist, false otherwise. Only one item in a picklist can be designated as the default.

 

 

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

This was selected as the best answer
MrTheTylerMrTheTyler

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

bohemianguy100bohemianguy100

Thank you! I appreciate the help.