Showing posts with label controller. Show all posts
Showing posts with label controller. Show all posts

Using Wrapper class for displaying records from two objects in a single table

Sometimes we need to display data from different objects (not related to each other) in a table. Best way to do that is using wrapper class. I am showing you an example where i am displaying records from contact and account object ( i am not using their r/ship, so it could be any two object)

Page:
<apex:page controller="Deleteme7_controller" >
<apex:pageBlock >
    <apex:pageblocktable value="{!final}" var="a">
            <apex:column value="{!a.acc.name}" /> 
            <apex:column value="{!a.con.name}"/>
            </apex:pageblockTable>
     </apex:pageBlock>
</apex:page>

Controller:

public with sharing class Deleteme7_controller {
List final3 = new list();
wrapclass wrapobject;
public Deleteme7_controller (){
list a= [select id,name from account];
list c = [select name,id from contact];
add(a,c);
}

public list getFinal(){
return final3;
}

public void add(list a,list b){
integer j=0;

public class wrapclass{
public account acc{get;set;}
public contact con{get;set;}

}
}

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 :



Testing PageReference in Controllers 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: Imaging you have a controller's pagereference method like the following:
public class TestingVFController_Controller{
public PageReference save(){
PageReference p=null;
if(this.qp==null || !'yy'.equals(this.qp)){
p=Page.pagenotfound;
p.getParameters().put('error','NoParam');
}
else{
p=Page.homepage;
}}


if qp parameter='yy' then this will redirect to /apex/homepage else /apex/pagenotfound.
for this test class would be

static testMethod void ControllerTest() {
// first test without parameter
PageReference p=Page.HomePage;
Test.setCurrentPage(p);
TestingVFController_Controller ctrl=new TestingVFController_Controller();
String nextPage=ctrl.save().getUrl();

System.assertEquals('/apex/pagenotfound?error=NoParam',nextPage);


//then check with parameter case

ApexPages.currentPage().getParameters().put('qp','yyyy');
ctrl=new TestingVFController_Controller();
nextPage=ctrl.save().getUrl();
System.assertEquals('/apex/homepage', nextPage);