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
OxeneOxene 

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)?

Best Answer chosen by Admin (Salesforce Developers) 
aebenaeben

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

aebenaeben

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.

 

 

This was selected as the best answer
OxeneOxene
Thanks for the reply Aeben. This might be the documentation that you are referring to above. This code is in Java and some of the objects they have referred to here (eg: describeLayoutResult) is not available in Apex. So in short, I don't have an equivalent to this code in Java. Do you have any idea regarding some sample code for the same logic?
aebenaeben

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.

aebenaeben
^^^ I copied the actual logic from a discussion in this forum.
richardvrichardv

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/

Message Edited by richardv on 04-24-2009 12:04 PM
AlsoDougAlsoDoug

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?

 

 

 

 

AlsoDougAlsoDoug

Never mind.

 

Had C instead of c.

 

Got too use to Sales Force not being case sensitive.

WilmerWilmer

Hi Richard,

 

Would you please tell me where is the code of the resource "prototype"? or is it a generic call?

<apex:includeScript value="{!$Resource.prototype}"/>

 

Regards,

 

Wilmer

heiriticheiritic

 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