1

My question is:

Is it possible to activate/deactivate picklist values via apex code? If so - how to do it?

EDIT I need to deactivate custom field picklist value on my custom object. I want use it in unit test.

  • This sounds like it may be an X-Y Problem. Deactivating a picklist value sounds like an attempted solution for the real problem you're trying to solve here. Can you go into more detail about what problem you're trying to solve by deactivating picklist values? – Derek F Oct 12 '17 at 13:22
  • My algorithm is as follows: load object from database -> get picklist value -> if value is active do A -> if value is inactive do B. I want to cover both cases in my unit tests if possible. – Łukasz Kojzar Oct 13 '17 at 6:55
2

To disable a value from <apex:selectOptions you have to use this signature SelectOption option = new SelectOption(value, label, isDisabled); where isDisabled is a Boolean that, if true, specifies that the user cannot select the option, but can still view it. Document reference

public List<SelectOption> getItems() {
    List<SelectOption> options = new List<SelectOption>();
    options.add(new SelectOption('US','US'));
    options.add(new SelectOption('CANADA','Canada',true));
    options.add(new SelectOption('MEXICO','Mexico'));
    return options;
}
|improve this answer|||||
  • Thanks a lot Himanshu, but it was not what I am looking for. :) My question was maybe not clear. I need to deactivate a value in the unit test for my custom field picklist on my custom object. I will edit the question. – Łukasz Kojzar Oct 12 '17 at 11:45
  • @ŁukaszKojzar That you can't do using apex. – Himanshu Oct 12 '17 at 12:39
  • Are you sure? May you deliver the information source? (e.g. link to the documentation) If so - I will accept the answer. :) – Łukasz Kojzar Oct 12 '17 at 13:09
  • you want to do it from test class right? – Himanshu Oct 12 '17 at 13:11
  • 2
    Picklist values can be accessed using tooling api (only active one) and from test class you can't make web callout and get response. By default, test methods don’t support web service callouts (developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…) – Himanshu Oct 12 '17 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.