function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
A MeA Me 

How to get picklist values for a field by record type , using apex only , no VF pages?

Hi,

I am writing a web service which will validate the incoming request and create a custom object record based on the parameters in the request. The custom object has 2 record types (say 'A' and 'B') . The web service creates records of record type 'A' only .
One of the fields of the object is a picklist , type__c , and the web service must validate the value of this field in the request. So it must check that the incoming value is one of the picklist values permissible for record type 'A' of the custom object.

I have checked several posts, but all of them involve visualforce pages and their controllers. How can this be achieved in apex, without VF pages?
Best Answer chosen by A Me
bob_buzzardbob_buzzard
The simple answer is that it can't - the schema describe apex classes can only give you the full set of picklist values rather than those for a specific record type.  Hence the use of VF, where you can inspect the rendered picklist and pull the appropriate values out.

You may be able to do this via the metadata API, and you can access this in an unsupported library from Financial Force at:

https://github.com/financialforcedev/apex-mdapi

This isn't endorsed or supported by Salesforce, so there is potential for it to stop working if Salesforce change the way that the metadata API works.

All Answers

bob_buzzardbob_buzzard
The simple answer is that it can't - the schema describe apex classes can only give you the full set of picklist values rather than those for a specific record type.  Hence the use of VF, where you can inspect the rendered picklist and pull the appropriate values out.

You may be able to do this via the metadata API, and you can access this in an unsupported library from Financial Force at:

https://github.com/financialforcedev/apex-mdapi

This isn't endorsed or supported by Salesforce, so there is potential for it to stop working if Salesforce change the way that the metadata API works.
This was selected as the best answer
A MeA Me
Thanks for the quick reply.