Syntax Highlighter JS

Thursday, March 1, 2012

How to get a list of picklist values enabled for a given record type.

What if you want to product a list of picklist values that are enabled for a certain record type?

Without major development (i.e. changing the WSDL), you can't access the Meta API in apex code and you can't use SOQL to query this kind of information. 
You could always use visualforce and the Ajax Tool kit or write your own program (in say java) to access the API but this is over kill for this kind of task.

There is an easier way to use the force to get this information out of the system. This post assumes you have the Eclipse IDE configured already for your system as I will use the Metadata logic built into it to pull this data.  Be sure to save any uncommitted changes and refresh your project before starting.

In this simple example, we have a custom picklist field called "State__c" and record type called "Western" on the Opportunity object and we want to get the values that are displayed on the screen to the left.

Open eclipse then right click on the root parent object in the packages tree and select properties.  Then expand the child and select project Contents.  Then Click the Add/Remove button.

When the "Choose Metadata Components" box comes up, click the refresh button as seen in the image on the right.  Once the refresh has been completed, go to either objects - standard or objects - custom.  In this example, we are using Opportunity which a standard object and state__c which is a custom field.  

Then select them as pictured on the left. Then hit Ok, then Ok again and finally yes to refresh the project from the server.

Once the refresh has been completed, locate the object from the tree.  By default it will open in the XML editor.  Right click on it and select open with -> text editor.

When the file opens, scroll down and locate the <recordTypes> section that has the <fullName> you are looking for (Western in this case).  Then Locate the <picklistValues> section that has the <picklist> with the field you want (State__c in this case). 

There you will find a <values> section that will contain *just* the picklist values enabled for that record type.   

Now you can copy that section out and use it as you will.  Most likely you will want to remove the XML tags with a mass find/replace to get a simple list.

No comments:

Post a Comment