+ Start a Discussion
Adil_SFDCAdil_SFDC 

Render a visualforce Page based on picklistvalues

Hi Experts,
I am trying to render a visualforce Page with the change in picklist values. The data I would like to render is the contact names in  a table. which changes depending on the sales Region i choose. 

Here is my page and controller so far. Any help is appreciated.
<apex:page controller="PhotoSchedulerController" tabstyle="Report" sidebar="false">
<apex:form >
<apex:outputPanel id="dataBlocks" >
<apex:sectionHeader title="Field Researcher Schedule for Region"/>

<apex:selectList value="{!contactSalesRegion}" size="1" multiselect="false">
 <b>Field Researcher Schedule for Region</b><apex:selectOptions value="{!SalesRegionsCoveredValues}" > </apex:selectOptions>
<apex:actionSupport event="onchange" action="{!salesRegionCoveredFilter}" reRender="dataBlock" > 
<apex:param name="salesRegionId" assignTo="{!salesRegionId}" value="{!SalesRegionsCoveredValues}"></apex:param>  
</apex:actionSupport> 
</apex:selectList>

<apex:pageBlock >
 
<table class="list" border="0" cellpadding="0" cellspacing="0"  id="dataBlock" >

  <tr>
     &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;
     &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;
     &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;
     &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;
     &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;
     &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;
     &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;
     &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;
     &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;

     <b>Scheduled Date</b>
 
  </tr>
    <tr>
      
      <td>
      </td>
      <apex:repeat value="{!headDays}" var="days">
        <th style="background-color:#00BFFF;">
            {!days}
        </th>
    </apex:repeat>
    </tr>
   
    <tr class="headerRow  ">
      <th style="background-color:#00BFFF;">Photographer: Full Name</th>
      
      <apex:repeat value="{!headDates}" var="heading">
        <th style="background-color:#00BFFF;" class="headerRow ">
           {!heading}
        </th>
      </apex:repeat>
    </tr>
    <apex:repeat value="{!DataRows}" var="dRow">
       <apex:repeat value="{!dRow.contacts}" var="photo">
           <tr>
               <td style="background-color:#d4dadc;">
                  <a href="https://apartments--pre.cs20.my.salesforce.com/{!photo.Id}"> {!photo.Name} </a>
               </td>
            
            <apex:repeat value="{!dRow.cases}" var="count">
           
                <td> 
                 {!count.casenumber}
                </td>
              
            </apex:repeat>

            </tr>
         </apex:repeat>
         
    </apex:repeat>
  </table>
  </apex:pageBlock>
  </apex:outputPanel>
    </apex:form> 
</apex:page>
public class PhotoSchedulerController {

    public String salesRegionId { get; set; }
    public String ContactSalesRegion {get;set;}

    public PageReference salesRegionCoveredFilter() {
    	String contacts =  [Select Id,Name,Sales_Regions_Covered__c from Contact where Sales_Regions_Covered__c= '03'].Sales_Regions_Covered__c;
    	         system.debug('sales region:'+contacts);
    	
        return null;
    }

    public  List<SelectOption> getSalesRegionsCoveredValues() {
       List<SelectOption> options = new List<SelectOption>();
        Schema.DescribeFieldResult salesRegion = Contact.Sales_Regions_Covered__c.getDescribe();
        List<Schema.PicklistEntry> salesRegions = salesRegion.getPicklistValues();
        for(Schema.Picklistentry entry : salesRegions){
            options.add(new SelectOption(entry.getLabel(),entry.getValue()));
        }
        return options;
    } 
    
    public List<String> getHeadDays() {
        List<String> formattedDays = new List<String>();
        List<DateTime> days = new List<DateTime>();
        DateTime currentDay = DateTime.now();
        for (Integer idx=0; idx<28; idx++){
            days.add(currentDay+idx);
        system.debug('DAYS :'+days);
        } 
        for(DateTime dt: days){
            String formattedDay = dt.format('E');
            formattedDays.add(formattedDay);
        }
        return formattedDays;
    }
    
    public List<String> getHeadDates(){
        List<String> formattedDates = new List<String>();
        List<Date> dates = new List<Date>();
        Date currentDate = system.today();
        for (Integer idx=0; idx<28; idx++){
            dates.add(currentDate+idx);
        }
       
        for(Date dt: dates){
            String formattedDate = dt.format();
            formattedDates.add(formattedDate);
           
        }
        return formattedDates;
        }
        
    // retrieves the row wrapper containing the wrapped case headings
    public List<photographerRow> getDataRows(){
        List<String> scheduledDates = new List<String>();
        photographerRow pRow = new photographerRow();
        List<photographerRow> dataRows = new List<photographerRow>();
        for(Contact con : getContacts()){
            pRow.photographer = con.Name;
            pRow.photographerId = con.Id;
            pRow.contacts.add(con);
        }
        for (Integer idx=28; idx>0; idx--) {
            pRow.subtotals.add(idx);
        }
        
        for(Case c : getCaseInfo()){
            pRow.caseNumber = c.caseNumber;
            pRow.cases.add(c);
        }
    
        dataRows.add(pRow);
        return dataRows;
    }
    
    public class photographerRow{
        public String photographer {get; set;}
        public String photographerId {get;set;}
        public String caseNumber{get;set;}
        public List<Contact> contacts {get;set;}
        public List<Case> cases {get;set;}
        public List<Integer> subtotals {get; set;}
        public List<Integer> counts {get; set;}
        // constructor
        public PhotographerRow(){
            contacts = new List<Contact>();
            subtotals=new List<Integer>();
            counts=new List<Integer>();
            cases = new List <Case>();
        }
    }
    
    
    public List<Contact> getContacts(){
        List<Contact>contacts =  [Select Id,Name,Sales_Regions_Covered__c from Contact ];
        return contacts;
    }
    

    
    public List<Case> getCaseInfo(){
        List<String> contactsList = new List<String>();
        List<String> formattedDatesList = new List<String>();
        for(Contact c :  getContacts()){
                        contactsList.add(c.Name);
        }
        List<Date> dates = new List<Date>();
        Date currentDate = system.today();
        for (Integer idx=0; idx<28; idx++){
            dates.add(currentDate+idx);
        }
        system.debug('Dates List :'+dates);
        system.debug('Contact LIst :'+contactsList);
        
        List<Case> cases = [Select id,CaseNumber,Scheduled_Date__c ,Account.Owner.Name,Photographer_Contact__r.Name from Case where RecordType.DeveloperName In('Time_Booking','Media_Package','Photography') 
                                                        and Scheduled_Date__c=:dates];
        system.debug('case queried:'+cases);
        return cases;   
    }   
    
  


    /* public Integer getCaseCount(){
        Integer caseCount;
        List<AggregateResult> groupedCases = [Select count(casenumber),Scheduled_Date__c, Photographer_Contact__r.Name from Case where RecordType.DeveloperName In('Time_Booking', 'Media_Package', 'Photography') and Scheduled_Date__c!=null group by Photographer_Contact__r.Name,Scheduled_Date__c];
        for (AggregateResult ar : groupedCases)  {
                caseCount = Integer.valueOf(ar.get('expr0'));
        }
    return caseCount;
    }*/
}
Thanks for the expert advice. 
Adil

AshlekhAshlekh
Hi 

Actually you have written this reRender="dataBlock" in your acctionsupport, but "datablock" is id of html component not id of apex component, We need to always pass the id of apex component and always pass this id of parent element of section which we want to rerender.

So I just make some small changes in your page, now you can use below code 
<apex:page controller="PhotoSchedulerController" tabstyle="Report" sidebar="false">
<apex:form >
<apex:outputPanel id="dataBlocks" >
<apex:sectionHeader title="Field Researcher Schedule for Region"/>

<apex:selectList value="{!contactSalesRegion}" size="1" multiselect="false">
 <b>Field Researcher Schedule for Region</b><apex:selectOptions value="{!SalesRegionsCoveredValues}" > </apex:selectOptions>
<apex:actionSupport event="onchange" action="{!salesRegionCoveredFilter}" reRender="dataBlockTableID" > 
<apex:param name="salesRegionId" assignTo="{!salesRegionId}" value="{!SalesRegionsCoveredValues}"></apex:param>  
</apex:actionSupport> 
</apex:selectList>

<apex:outPutPanel id="dataBlockTableID">

<apex:pageBlock >
 
<table class="list" border="0" cellpadding="0" cellspacing="0"  id="dataBlock" >

  <tr>
     &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;
     &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;
     &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;
     &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;
     &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;
     &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;
     &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;
     &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;
     &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;

     <b>Scheduled Date</b>
 
  </tr>
    <tr>
      
      <td>
      </td>
      <apex:repeat value="{!headDays}" var="days">
        <th style="background-color:#00BFFF;">
            {!days}
        </th>
    </apex:repeat>
    </tr>
   
    <tr class="headerRow  ">
      <th style="background-color:#00BFFF;">Photographer: Full Name</th>
      
      <apex:repeat value="{!headDates}" var="heading">
        <th style="background-color:#00BFFF;" class="headerRow ">
           {!heading}
        </th>
      </apex:repeat>
    </tr>
    <apex:repeat value="{!DataRows}" var="dRow">
       <apex:repeat value="{!dRow.contacts}" var="photo">
           <tr>
               <td style="background-color:#d4dadc;">
                  <a href="https://apartments--pre.cs20.my.salesforce.com/{!photo.Id}"> {!photo.Name} </a>
               </td>
            
            <apex:repeat value="{!dRow.cases}" var="count">
           
                <td> 
                 {!count.casenumber}
                </td>
              
            </apex:repeat>

            </tr>
         </apex:repeat>
         
    </apex:repeat>
  </table>
  </apex:pageBlock>
 </apex:outputPanel> 
  </apex:outputPanel>
    </apex:form> 
</apex:page>


Adil_SFDCAdil_SFDC
Hi Ashlekh, 

Thanks for the response. Do you think my select list and list options work great with the param i am using. I am confused as they dont display result as desired.
AshlekhAshlekh
Hi,

public PageReference salesRegionCoveredFilter() {
    	String contacts;
		//The query may be return more than one record 
		for(Contact c: [Select Id,Name,Sales_Regions_Covered__c from Contact where Sales_Regions_Covered__c= '03'])
    	{
			contacts = c..Sales_Regions_Covered__c;
		}         system.debug('sales region:'+contacts);
    	
        return null;
    }

Page 
<b>Field Researcher Schedule for Region</b>
	<apex:selectList value="{!contactSalesRegion}" size="1" multiselect="false">
		<apex:selectOptions value="{!SalesRegionsCoveredValues}" /> 
		<apex:actionSupport event="onchange" action="{!salesRegionCoveredFilter}" reRender="dataBlock" > 
			<!--	
					Why are using param ,you are assigning in salesRegionId all  SalesRegionsCoveredValues value it not make sense
					<apex:param name="salesRegionId" assignTo="{!salesRegionId}" value="{!SalesRegionsCoveredValues}"></apex:param>
			-->  
		</apex:actionSupport> 
	</apex:selectList>


Adil_SFDCAdil_SFDC
Thank you for your quick response

I guess i wasnt able to explain my requirement .
With the select options i got all the picklist values in the CONTACT object multiselect picklist field sales_region_covered__c.
Now when I choose the picklist value ( 01,02,03,04 thru 16) I need to see a different  list of contacts on pageblock.
Thats why i am using param to get my selected  picklist values and pass to my soql. I am trying to get the selected picklist value in my SOQL  via salesRegionID
 <apex:param name="salesRegionId" assignTo="{!salesRegionId}" value="{!SalesRegionsCoveredValues}"></apex:param>
That query was just part of my testing. 
Adil_SFDCAdil_SFDC
Hi I have made changes . But i have hardcoded the value . I am not sure how to get it dynamic. The value which is bold in the code is hardcoded to 06. 
<apex:sectionHeader title="Field Researcher Schedule for Region" />

            <apex:selectList value="{!contactSalesRegion}" size="1" multiselect="false">
                <b>Field Researcher Schedule for Region</b>
                <apex:selectOptions value="{!SalesRegionsCoveredValues}"></apex:selectOptions>
                <apex:actionSupport event="onchange" action="{!salesRegionCoveredFilter}" reRender="dataBlock">
                    <apex:param name="salesRegionId" assignTo="{!salesRegionId}" value="06"></apex:param>
                </apex:actionSupport>
            </apex:selectList>
            
            <apex:outputPanel id="dataBlock">
            <apex:pageBlock >
					 
        
                        <b>Scheduled Date</b>
                   
                <table class="list" border="0" cellpadding="0" cellspacing="0">

Here is the controller and in my salesRegionCoveredFilter() method i get debug as 06 since hardcoded. how to get the selected list value.
public class PhotoSchedulerController {

    public String salesRegionId { get; set; }
    public String ContactSalesRegion {get;set;}
    public List<Contact> contactsToDisplay {get;set;}
        
    public void salesRegionCoveredFilter() {
    	
        system.debug('sales Region ID:'+salesRegionId);// HERE I NEED  SELECTED VALUES FROM PICK LIST
       // getDataRows();
    }
    

    public  List<SelectOption> getSalesRegionsCoveredValues() {
       List<SelectOption> options = new List<SelectOption>();
       Schema.DescribeFieldResult salesRegion = Contact.Sales_Regions_Covered__c.getDescribe();
       List<Schema.PicklistEntry> salesRegions = salesRegion.getPicklistValues();
       for(Schema.Picklistentry entry : salesRegions){
            options.add(new SelectOption(entry.getLabel(),entry.getValue()));
        }
        return options;
    }