Creating dependant pickist in visualforce from custom settings

Creating dependant pickist in visualforce from custom settings

Hi,
Today, i got a requirement for creating a dependant picklist which gets its values from custom setting. I thought of sharing it with you all.

In my example, i am selecting name of countries and its associated city from custom settings.

Steps :
1) Create a new list type custom setting, with only one text field named 'City' in my case.
2) Click on manage in the custom setting, and create a new record.
Name of the custom setting record should be name of the country.
City field contains name of the cities for that country, separated by ';'



3) Code for page :



<apex:page controller="CustomSettings_controller" >
<apex:pageBlock >
<apex:form >
<apex:actionFunction name="rerenderStates" rerender="ListState" >
<apex:param name="firstParam" assignTo="{!country}" value="" />
</apex:actionFunction>

<apex:pageblocksection >
Country <apex:selectList id="ListCountry" value="{!country}" size="1" onchange="rerenderStates(this.value)">
<apex:selectOptions value="{!countrylist}"></apex:selectOptions>
</apex:selectList>
</apex:pageblocksection>

<apex:pageblocksection >
State <apex:selectList id="ListState" value="{!states}" size="1">
<apex:selectOptions value="{!statelist}"></apex:selectOptions>
</apex:selectList>
</apex:pageblocksection>

</apex:form>
</apex:pageBlock>
</apex:page>

4) code for controller :




public class CustomSettings_controller {


Map<String, countrystate__c> c = countrystate__c.getall();


public list<selectoption> getStateList() {
list<selectoption> s = new list<selectoption> ();
s.add(new selectoption('','Select one'));
if(country==null){
country='';
}
else
if(c.containskey(country)){
s.clear();
string sm = c.get(country).States__c;
for(string st: sm.split(';'))
s.add(new selectoption(st,st));
}

return s;
}


public List<SelectOption> getCountrylist() {
List<SelectOption> country = new List<SelectOption>();
country.add(new selectoption('India','---SELECT ONE---'));
list<string> countryname= new list<string>();
countryname.addall(c.keyset());

for(string s : countryname){
country.add(new selectoption(s,s));

}

return country;
}


public String states { get; set; }

public String country{ get; set;}

}


Final Outcome :



Test a controller in salesforce

Some developers have become frustrated when trying to write unit tests for their Controllers To be fair, if you don’t know what base you assertions on, it can be hard to test the code. So here are a couple of quick tips for getting it done:
suppose you have a controller like this


public with sharing class TestingVFController_Controller {

private string qp;

public TestingVFController_Controller(){
this.qp=ApexPages.currentPage().getParameters().get('qp');
}

public string FirstName {
get;set;}

public string LastName {
get;set;
}
public string email {
get;set;
}
public string Company {
get;set;
}

public PageReference save(){
PageReference p=null;
if(this.qp==null || !'yyyy'.equals(this.qp)){
p=Page.filenotfound;
p.getParameters().put('error','NoParam');

}
else{
try{
Lead l = new Lead(Firstname=this.firstName,LastName=this.LastName,company=this.Company, email=this.email);
insert l;
}
catch(Exception e)
{
p=Page.filenotfound;
p.getParameters().put('error','insertFailure');
}

}
if(p==null)
p=Page.homepage;

p.setRedirect(true);
return p;
}
}

Solution for "INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session"


System.out.println("LOGGING IN NOW....");
enterpriseConnection connection = new enterpriseConnection();
loginResult = connection.login(userName, Password);
System.out.println("Succesfully logged IN");
connection._setProperty(SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY, loginResult.getServerUrl());
SessionHeader sessionHdr = new SessionHeader();
sessionHdr.setSessionId(loginResult.getSessionId());
connection.setHeader(new SforceServiceLocator().getServiceName().getNamespaceURI(),"SessionHeader", sessionHdr);

resolution for "the url returned from login must be set in the sforceservice "


EnterpriseConnection connection = new enterpriseConnection();
loginResult = connection.login(userName, Password);
System.out.println("Succesfully logged IN");
connection._setProperty(SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY,loginResult.getServerUrl());
SessionHeader sessionHdr = new SessionHeader();
sessionHdr.setSessionId(loginResult.getSessionId());
connection.setHeader(new SforceServiceLocator().getServiceName().getNamespaceURI(),"SessionHeader", sessionHdr);