2

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.

  • PicklistEntry class has isActive() method, that cannot be used? – Raul May 11 '18 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 May 11 '18 at 9:08
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
}
  • 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 May 11 '18 at 9:02
1

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.

  • I think from Summer 18 changes it will not hit the limit. – Tushar Sharma May 11 '18 at 11:13
  • 1
    @TusharSharma Thanks for the link: seems to be about COUNT only? – Keith C May 11 '18 at 12:35
  • Yes it looks like as of now it support COUNT only. – Tushar Sharma May 11 '18 at 12:46
0

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

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.