Working with Dependent Picklists II

This scenario is where you need to auto populate a picklist field value based on another picklist in Salesforce, while, for example, creating a new Lead record. The picklists are inter-related in Salesforce (called "dependent"). You can determine if a picklist is dependent with:

if(picklistField.dependentPicklist)

To determine the allowed values see “About dependent  picklist’s” from here. This describes how you find the valid picklist values based on the source picklist’s options. (Note that .NET has BitArray so BitSet mentioned in the article will be unnecessary). Below is a simple test to retrieve dependent picklist values:

[TestMethod]
public void GetPicklistValues()
{
	 SalesforceSession salesforceSession = SessionTest.GetActiveSessionS4S();
	 string fieldName = "TestPickList__c";
	 
	 FieldService contactFieldService = FieldService.Instance(Contact.SFType, salesforceSession);
	 SalesforceField picklistField = contactFieldService.FieldFromObject(fieldName);
	 
	 //Determine if field is dependant picklist
	 Assert.IsTrue(picklistField.dependentPicklist);
	 Assert.IsNotNull(picklistField, "Failed to find Contact field: " + fieldName);
	 Assert.AreEqual(fieldType.picklist, picklistField.type, "field is expected to be picklist");
	 
	 foreach (PicklistEntry picklistEntry in picklistField.picklistValues)
	 {
		   //We have the the bits determining if the value is valid just requires parsing
		   BitArray bits = new BitArray(picklistEntry.validFor);
	 }
}