Retrieve picklist value of a Record Type using MetaData Service in Salesforce
Picklist values are able to see using standard UI of sObject then field name and then record type name. What if we want to see these picklist values using a Visualforce page.
Here is the solution to this situation using the Metadata Service Class. By using this class we can get sObject as metadata object and retrieve its recordtype, list of picklist values or particular picklist values.
So how to enable this functionality in Salesforce? Just follow the steps : –
1. Create a Remote Setting which holds URL of your org
Firstly, search for Remote site setting in the Quick Find box.
Now click on New Remote Site
Now Enter details of the Remote site setting, in which Remote Site URL is your instance from the URL.
For example: https://*********-dev-ed—c.ap4.visual.force.com
2.Set Session Settings to "Lock session to the domain in which they were first used"
3. Write a Metadata Service class which is used to get dependent picklist values on the basis of record type.
Here is the code snippet:
Apex Controller:
public class retrievePicklistValueofRecordType(){ public String objectName{get;set;} public String recordTypeName{get;set;} public String fieldName{get;set;} public List<String> listofValue{get;set;} public retrievePicklistValueofRecordType() { objectName = ”; recordTypeName = ”; fieldName = ”; listofValue = new List<String>(); } public void retrieveValue(){ listofValue=MetadataServiceExamples.getDependentPicklistValuesByRecordType(objectName,recordTypeName,fieldName); } }
Metadata Service Class:
public with sharing class MetadataServiceExamples { public static List<String> getDependentPicklistValuesByRecordType(String objectName,String recordTypeName,String fieldName){ List<String> listofavailablevalue = new List<String>(); String objectRecordType = objectName + '.' + recordTypeName; // Create serviceMetadataService.MetadataPort service = createService(); MetadataService.RecordType recordType = (MetadataService.RecordType) service.readMetadata('RecordType', new String[] { objectRecordType }).getRecords()[0]; for ( MetadataService.RecordTypePicklistValue rpk : recordType.picklistValues ) { if ( rpk.picklist == fieldName ) { for ( MetadataService.PicklistValue pk : rpk.values ) { listofavailablevalue.add(String.valueof(pk.fullName)); } } } return listofavailablevalue; } }
public MetadataService.IReadResult readMetadata(String type_x,String[] fullNames) { MetadataService.readMetadata_element request_x = new MetadataService.readMetadata_element(); request_x.type_x = type_x;request_x.fullNames = fullNames; MetadataService.IReadResponseElement response_x; Map<String, MetadataService.IReadResponseElement> response_map_x = new Map<String,MetadataService.IReadResponseElement>(); response_map_x.put('response_x',response_x); WebServiceCallout.invoke(this,request_x,response_map_x,new String[ { endpoint_x,'','http://soap.sforce.com/2006/04/metadata','readMetadata','http://soap.sforce.com/2006/04/metadata','readMetadataResponse','MetadataService.read' + type_x + 'Response_element'}); if(!Test.isRunningTest()){ return response_x.getResult(); } else { MetadataService.IReadResult obj;return obj; } }
Here is the Picklist value: List of Picklist value
Particular Picklist value:
Hope this will help you.
Thanks.