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
❤Code❤Code 

Dynamic Apex for display dependent picklist in vf page

Hi All,
I have two picklist fields Category__c and SubCust__c(dependent on Category__c field). How to display this field values using dynamic apex.
Need help how to display as dependent picklist.

User-added image
Regards
Mudasir WaniMudasir Wani
Hello,

Here is a blog on this 

https://gist.github.com/boxfoot/4166342
 
/**
 * getDependentPicklistOptions
 * by Benj Kamm, 2012
 * (inspired by http://iwritecrappycode.wordpress.com/2012/02/23/dependent-picklists-in-salesforce-without-metadata-api-or-visualforce/)
 * CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/us/)
 *
 * Build an Object in which keys are valid options for the controlling field
 * and values are lists of valid options for the dependent field.
 *
 * Method: dependent PickListEntry.validFor provides a base64 encoded
 * string. After decoding, each of the bits (reading L to R)
 * corresponds to the picklist values for the controlling field.
 */
function getDependentOptions (objName, ctrlFieldName, depFieldName) {
	// Isolate the Describe info for the relevant fields
	var objDesc = sforce.connection.describeSObject(objName);
	var ctrlFieldDesc, depFieldDesc;
	var found = 0;
	for (var i=0; i<objDesc.fields.length; i++) {
		var f = objDesc.fields[i];
		if (f.name == ctrlFieldName) {
			ctrlFieldDesc = f;
			found++;
		} else if (f.name == depFieldName) {
			depFieldDesc = f;
			found++;
		}
		if (found==2) break; 
	}

	// Set up return object
	var dependentOptions = {};
	var ctrlValues = ctrlFieldDesc.picklistValues;
	for (var i=0; i<ctrlValues.length; i++) {
		dependentOptions[ctrlValues[i].label] = [];
	}

	var base64 = new sforce.Base64Binary("");
	function testBit (validFor, pos) {
		var byteToCheck = Math.floor(pos/8);
		var bit = 7 - (pos % 8);
		return ((Math.pow(2, bit) & validFor.charCodeAt(byteToCheck)) >> bit) == 1;
	}
	
	// For each dependent value, check whether it is valid for each controlling value
	var depValues = depFieldDesc.picklistValues;
	for (var i=0; i<depValues.length; i++) {
		var thisOption = depValues[i];
		var validForDec = base64.decode(thisOption.validFor);
		for (var ctrlValue=0; ctrlValue<ctrlValues.length; ctrlValue++) {
			if (testBit(validForDec, ctrlValue)) {
				dependentOptions[ctrlValues[ctrlValue].label].push(thisOption.label);
			}
		}
	}
	return dependentOptions;
}


var OBJ_NAME = 'Custom_Object__c';
var CTRL_FIELD_NAME = "Controlling_Field__c";
var DEP_FIELD_NAME = "Dependent_Field__c";
var options = getDependentOptions(OBJ_NAME, CTRL_FIELD_NAME, DEP_FIELD_NAME);
console.debug(options);

Donot forget to select best answer to make our efforts visible in the developer forum.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help

 
Alok Khandelwal - DamcoAlok Khandelwal - Damco
Hi sdfc1.buddy,

You could do that just as usual by configuring the Field Dependencies in Object configuration and using inputField controls in the visual page inside a pageblocksection. Refer to the article below for more details: https://www.salesforce.com/docs/developer/pages/Content/pages_quick_start_dependent_picklists.htm

I hope it was helpful.

Regards,
Alok
❤Code❤Code
Hi Alok, 

I need to have multiselect hence i am using the below code - 

    public List<SelectOption> getDept()
        {
            List<SelectOption> options = new List<SelectOption>();
            Schema.DescribeFieldResult departfield =  Activity_Tracker__c.Category__c.getDescribe();
            List<Schema.PicklistEntry> picklist = departfield.getPicklistValues();
            for( Schema.PicklistEntry f : picklist){      
            options.add(new SelectOption(f.getLabel(), f.getValue()));
            }    
            return options;
        }



    public List<SelectOption> getCustb()
        {
            List<SelectOption> options = new List<SelectOption>();
            Schema.DescribeFieldResult departfield =  Activity_Tracker__c.SubCust__c.getDescribe();
            List<Schema.PicklistEntry> picklist = departfield.getPicklistValues();
            for( Schema.PicklistEntry f : picklist){      
            options.add(new SelectOption(f.getLabel(), f.getValue()));
            }    
            return options;
        }

Can u please check how to make it dependent.

Regards