I recently came across an interesting problem mentioned in this post by Mr. Keir Bowden(@bob_buzzard), solution mentioned in this post was cool and it excited me to try an alternate one that started drawing in my mind, while I was reading his post.
The Alternate Solution (or generic API)
Using the same approach used by Keir, my goal was to come up with an API that can work with any Sobject and picklist field. The client code requiring a picklist describe needs to deal with only one class named “PicklistDescriber” and use one of the 3 describe() calls as explained below:
- When you have the record id
// Query or Load the Account as required Id accountId = [Select Id from Account Where Name = 'Most Paying Customer']; // Here is the Describe call, the resultant List<string> is all picklist values List<string> options = PicklistDescriber.describe(accountId, 'Industry');
- When you know the sobjectType and recordType name
// 'Record_Type_1' is a sample record type name on Account List<string> options = PicklistDescriber.describe('Account', 'Record_Type_1', 'Industry'));
- When you know the sobjectType and recordTypeId
// 'Record_Type_2' is a sample record type name on Account Id recType2Id = [Select Id from RecordType Where SobjectType = 'Account' AND DeveloperName like 'Record_Type_2'].Id; List<string> options = PicklistDescriber.describe('Account', recType2Id, 'Industry');
You can copy the required metadata from this github project, if you are interested in internals about this API. All the required setup and usage information is mentioned in the README markdown file at the project home page.
If, you are interested in internals, please continue reading below.
How this works ?
The key ingredients to my approach were
- Keir’s approach to render picklist values in a visualforce page, i.e. use VF intelligence for rendering the picklist values based on record type correctly.
- Use Dynamic Visualforce bindings, to make it work for any sobject.
Below diagram describes the communication between the components. Please refer this github repository for source code.
Please click on this image for bigger picture.
Your views and thoughts
Will be happy to discuss and looking forward for the same.
- @bob_buzzard’s post for original idea: http://bobbuzzard.blogspot.com/2012/01/record-type-picklist-values.html
- Github repository for my approach : https://github.com/abhinavguptas/Multi-RecordType-Sobject-Picklist-Describer