本篇引用如下三個連接:html
http://www.tgerm.com/2012/01/recordtype-specific-picklist-values.html?m=1git
https://github.com/abhinavguptas/Multi-RecordType-Sobject-Picklist-Describergithub
http://bobbuzzard.blogspot.com/2012/01/record-type-picklist-values.htmlide
咱們知道,record type能夠設置不一樣的profile針對類型爲picklist的字段顯示不一樣的values,可是apex的API中,經過schema取出的Picklist的values爲所有的值,並不能直接經過指定的方法取出指定的values。經過上述三個連接能夠很好的解決。this
原理圖以下:url
用法:spa
1.經過Record ID獲取其對應的record type name 以及類型爲picklist的field values。3d
2.經過sObjectType,Record Type Name以及field Name 獲取類型爲picklist的field values。code
其實,每一個customer object都會有一個recordtype字段,用來記錄當前的record的record type id。因此若是僅僅想在頁面上經過record id獲取當前record 具備的field 的picklist values,只須要用<apex:inputField>來盛裝此字段便可.inputField能夠根據字段的類型顯示不一樣的樣式。orm
PicklistController:經過URL傳遞的不一樣參數進行不一樣的處理,點擊按鈕後顯示所選的項的value
public with sharing class PicklistController { public Goods__c goods {get;set;} public String pickListFieldName {get;set;} public PicklistController() { Map<String, String> req = ApexPages.currentPage().getParameters(); PageReference ref = ApexPages.currentPage(); //設置當前的頁面開發模式開啓 //ref.getParameters().put('core.apexpages.devmode.url', '1'); String goodsId = req.get('id'); this.pickListFieldName = req.get('picklistFieldName'); String recordTypeId = req.get('recordTypeId'); goods = new Goods__c(); if(goodsId != null) { goods = [SELECT GoodsBrand__c, RecordTypeId FROM Goods__c where Id=:goodsId limit 1]; } if(goods.RecordTypeId == null) { goods.put('RecordTypeId', recordTypeId); } } public void showSelectedValue() { ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.INFO,goods.GoodsBrand__c)); } }
PickListDemoPage:顯示相關record type對應的picklist items以及button顯示所選擇的picklist的value值
<apex:page controller="PicklistController" showHeader="false" sidebar="false" > <apex:form > <apex:pageMessages /> <apex:inputField value="{!goods[pickListFieldName]}"/> <apex:commandButton action="{!showSelectedValue}" value="顯示所選值"/> </apex:form> </apex:page>
效果:
1.經過record id獲取此record的record type應該具備的picklist values
2.經過record type id獲取相關字段應該具備的picklist values
總結:若是須要在apex中獲取相關的picklist values而後進行相關後續處理能夠參看上方引用的連接以及源碼,若是隻是想要頁面中顯示相關使用record type的picklist values,則能夠對相關object配置相關的record type id,前臺使用apex:inputField便可。篇中有錯誤歡迎指出,有問題歡迎留言。