How to check picklist values with Apex in Salesforce
5 min
If you go to a restaurant and are asked to order, a very effective way to help you make a quick and informed decision is to give you a menu card to choose from.
A picklist is nothing but like a menu card that shows the user what all options (values) they have to choose from for that particular field. It provides the user with an input field which is read-only and accompanied by a drop-down list of predefined values. A picklist is not a smooth sail for all situations, especially when values change frequently and are not unique or when the number of values is too high.
- But there are many situations where they have proved to be of great help. Its advantages include:
- Simplifies data entry
- Standardize values (Managed by the administrator)
- Reduce changes of wrong values added
Picklists
In Salesforce there are three types of Picklist available:
Standard: Those are included in your Salesforce org before any customization.
Custom: Those created by users.
Custom Multi-Select: Allows users to choose multiple values.
Picklist can be easily seen in Salesforce Lightning mode by using the UI:
Setup -> Object Manager -> Select the Object where the field is -> Click on Fields and Relationships -> Select the field -> Scroll Down and you will see the values.
The picklist has two parts:
Field
Fields define the type of picklist and are displayed as a drop-down list. Users can configure one of such items as the default item.
- They can have the following properties:
- Restricted (limits users from adding new values)
- Dependent or Controlling ( filters values for one picklist based on a selection from another picklist)
The value set or Global picklist
A value set is a restricted picklist, where adding or modifying values are available only for Salesforce admins. A value set can help specify the company’s broadly common details such as specific country codes, or product properties.
- Regarding its limitations:
- 1000 values including both active and inactive are available per value set.
- Each company can have up to 500 global Picklist value sets.
So, what is Apex in Salesforce?
There are times when there is a need to design or check a picklist with Apex in Salesforce. Don’t worry this read will help you understand this term too. So let’s get the ball rolling….
Apex is a strong typed object-oriented programming language that is used to develop applications on top of the Salesforce platform. Using Apex users can write the custom business logic on the top of Salesforce including button clicks, related record updates, and Visualforce pages. It helps developers to execute flow and transaction control statements on Salesforce servers in alliance with calls to the API.
It is available in Salesforce Classic (not available in all orgs) and Lightning Experience; and in Enterprise, Performance, Unlimited, Developer, and Database.com Editions.
Using Picklist in Apex:
Dynamic Apex is used commonly for accessing picklist values from a server controller or a trigger. Along with its use in dynamic SOQL, it lets users analyze objects and fields with methods from the Schema namespace. Using this technique we can know the fields to an object, type of field, or values of the picklist field. Few of the instance methods related to PicklistEntry to get this information are:
Using the above methods we can retrieve picklist values with Apex in Salesforce. It is combined with getDescribe() and getPicklistvalues() methods to check the values of picklist.
If we have to check on the values of “Contact” object with the “Type” field that is a list. Sample code would look something like the following
The sample code above works until either a user wants to get only active values or a developer wants generic code. In the first case, the code above will return inactive values also. In the latter, to cover the aforementioned requirements in the code, it would be necessary to implement the following changes to the code:
Note about dynamic Apex: Dynamic Apex does not support the situation where the admin has restricted picklist values for a given record type. In the situation where this is the case, UI API endpoint needs to be called:
One of the reusable utility classes that are provided in Apex and worth mentioning is Picklist Utils. It’s simple to call and handle mostly the entire picklist use cases.
Using picklist in Lightning Web Components
In addition to above mentioned Apex controllers, there is a simpler solution to this. Apex is not required and a direct call to UI API with the get Picklist Values wire adapter from the:
lightning/uiObjectInfoApi module can be made.
To summarize a few things that would be useful for you, remember
- Dynamic Apex helps when there are records with no record type.
- UI API callouts help when using record types in Apex.
- UP API getPicklistValues wire adapter helps when working with Lightning Web Components.
- Apex utility class helps when to avoid code duplication.