You need to sign in to do that
Don't have an account?
Dependent picklist in visualforce
Hi Community,
I'm creating a visualforce page on opportunity object. My Stage values are dependent on Record type in Opportunity. I need to display the Stage picklist on this new visualforce page but it should contain only the valid picklist values for the particular record type of the Opportunity record. How can I achieve this?
I found this article in which the author is making use of two new custom objects to store the dependency information but I don't want to use this since it is going to be very difficult to maintain it. Is there any workaround for this (using the Metadata API or something)?
There is not straightforward solution for this issue.
You have to do it using a describe call and then looking at the bits set on a dependent pick list option. If i remember correctly, there is a sample in Apex Lang Ref or in Webservice Ref. Hope it helps.
All Answers
There is not straightforward solution for this issue.
You have to do it using a describe call and then looking at the bits set on a dependent pick list option. If i remember correctly, there is a sample in Apex Lang Ref or in Webservice Ref. Hope it helps.
You are right. Its not available in apex. You have to do it using ajax. That is, make a describe call from ajax.
Here is the code snippet: Since Describe calls are expensive, I built a map like data structure in Java Script and cached everything.
function isDependentValueValidForSelectedParent(parentSelIndex, depValue)
{
var b64 = new sforce.Base64Binary("");
var validFor = depValue.validFor;
var decodedVF = b64.decode(validFor);
var bits = decodedVF.charCodeAt(parentSelIndex >> 3);
if((bits & (0x80 >> (parentSelIndex%8))) != 0)
{
return true;
}
return false;
}
// obj : Name of the object where both the master and dependent picklist or attributes.
// controllerFieldName : Name of the master picklist field in the Object
// selIndex : Index of the selected option in the master picklist
// depFieldName : Name of the dependent picklist field in the Object
// depElementId : Id of the dependent picklist element in the page
function loadDependentPicklist(obj, controllerFieldName, selIndex, depFieldName, depElementId)
{
var objDescribe = sforce.connection.describeSObject(obj);
var depField = null;
for (var i=0; i<objDescribe.fields.length; i++)
{
if (objDescribe.fields[i].type == "picklist" && objDescribe.fields[i].name == depFieldName && objDescribe.fields[i].controllerName == controllerFieldName)
{
depField = objDescribe.fields[i];
break;
}
}
if (depField != null)
{
var values = depField.picklistValues;
for (var i=0; i<values.length; i++)
{
var label = values[i].label;
var value = values[i].value;
if (isDependentValueValidForSelectedParent(selIndex, values[i]) == true)
{
//If it comes here, then the current option is a dependent for the selected master.
}
}
}
}
Hope it helps.
Hi All, I've developed the following javascript/prototype class for this problem. See this blog entry:
http://richardvanhook.wordpress.com/2009/04/24/dependent-picklists-in-visualforce/
Richard,
Thanks for the DependentPicklistHelper you posted on your blog.
I am trying to use it but am getting the following Java Script error on page load:
"dependentFieldMetadata is undefined"
Here is my current code.
<apex:page id="page" standardController="Log__c" > <apex:includeScript value="{!$Resource.prototypeJS}"/> <apex:includeScript value="/soap/ajax/15.0/connection.js"/> <apex:includeScript value="{!$Resource.DependentPicklistHelperJS}"/> <apex:form id="form"> <apex:inputField onclick="helper.handleControllerChange()" id="controllingField" value="{!Log__c.Type__c}"/> <apex:selectList id="dependentField" value="{!Log__c.Program_Component__c}"> <apex:selectOption itemValue="None" itemLabel="--None--"/> </apex:selectList><p/> </apex:form> <script type="text/javascript"> var helper; document.observe('dom:loaded', function(){ helper = new DependentPicklistHelper( '{!$Api.Session_ID}','Log__c', 'Type__c','Program_Component__C', 'page:form:controllingField','page:form:dependentField' ); }); </script> </apex:page>
I am using a custom object instead of a standard one.
I am assuming when you said prototype you meant http://www.prototypejs.org/
Any quick ideas on what I have missed on a Friday before a long weekend?
Never mind.
Had C instead of c.
Got too use to Sales Force not being case sensitive.
Hi Richard,
Would you please tell me where is the code of the resource "prototype"? or is it a generic call?
Regards,
Wilmer
Hi, Just want to know for sure, is apex class can do dependent picklist ? I have read about javascript to do the dependent picklist process. Just want to make sure if there is easier method.
thanx
regards,
edwin