6

Here is my code. I'm trying to read Picklist Values via code, but I also intend to find out all Inactive Values on the field as well, but those values are not getting called in below code:

list<SelectOption> picklistItems = new list<SelectOption>();
map<String, Schema.SObjectField> fMap       = Schema.Task.getSObjectType().getDescribe().fields.getMap();
string fName = 'Status';
if (fMap.get(fName) != null) {
    Schema.DescribeFieldResult FR       = fMap.get(fName).getDescribe();
    if (FR.isAccessible()) {
        if (FR.getType() == Schema.DisplayType.Picklist) {
            for (Schema.PicklistEntry ple : FR.getPicklistValues()) {
                picklistItems.add( new SelectOption(ple.getLabel(), ple.getValue()) );
            }
        }
    }
}

Is there a way I can retrieve full picklist values via some code?

NOTE: The InActive values in my case here are the values which user has created records with, from APEX, the values are his own custom ones coming from code. I mean, you know we can save any custom picklist value from Apex, even though the value is not part of Picklist definition from package developer, but Salesforce allows user to be able to save records without any issues, and the value now becomes part of picklist but shown as Inactive values when we open the field in object explorer.

2
  • PicklistEntry class has isActive() method, that cannot be used?
    – Raul
    Commented May 11, 2018 at 8:53
  • I've updated my question with a NOTE, in which I'm trying to explain how the InActive values were created by customer and are not retrieved by describe call, thus cannot filter by isActive().
    – VarunC
    Commented May 11, 2018 at 9:08

3 Answers 3

5

Vote for idea https://success.salesforce.com/ideaView?id=0873A0000003biYQAQ to have Salesforce implement this, for the current release there is no way to obtain inactive picklist values

3

You can use an aggregate query to collect the values stored in the picklist field:

Set<String> values = new Set<String>();
for (AggregateResult ar : [
        select Status s
        from Task
        group by Status
        ]) {
    values.add(ar.get('s'));
}

and then difference that set from the ones you get from the describe information.

But you may run into the 50,000 rows governor limit and so have to move to running asynchrnous code that caches the results somewhere.

4
  • I think from Summer 18 changes it will not hit the limit. Commented May 11, 2018 at 11:13
  • 1
    @TusharSharma Thanks for the link: seems to be about COUNT only?
    – Keith C
    Commented May 11, 2018 at 12:35
  • Yes it looks like as of now it support COUNT only. Commented May 11, 2018 at 12:46
  • Also, if you only need to check whether a field's value is active or not, if it's not in getPicklistValues, it's not active! :-D Commented Feb 9, 2023 at 2:13
2

Your code is correct and using this code you will get all value. If you want you to filter Active, inactive value in code then you can use isActive()

Schema.DescribeFieldResult F = Account.Industry.getDescribe();
List<Schema.PicklistEntry> P = F.getPicklistValues();
if(p.isActive())
{
    //do something
}
else
{
    //do something
}
2
  • 3
    That's the trouble. I was also assuming that it will, but the Describe call is returning only Active values. I've updated my question with a NOTE, which I'm trying to explain how the InActive values were created by customer.
    – VarunC
    Commented May 11, 2018 at 9:02
  • Currently as of 2023-02-08: if(p.isActive()) { // Always true } else { // Never true } Commented Feb 9, 2023 at 2:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .