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
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student 

Help with extension and VF page for filtering between two dates

Hey there,

I have found some code on the internet, it is close..it is just not there yet. I have been able to filter a pageblocktable, from a page with a standard controller and the extension. The goal now is to be able to filter the records that are a child to a master object. This is what I have so far:

VF page:

<apex:page standardController="Office__c"  extensions="SearchExt" >



<apex:pageBlock title="Office Commissions"  >
<apex:form >
<apex:pageBlockSection >
                 <apex:pageBlockTable value="{!Office__c.Office_Commissions__r}" var="OffCom" >
                     <apex:outputLabel value="Start Date" />
                     <apex:inputField value="{!OffCom.Search_Start_Date__c}"/>
                 </apex:pageBlockTable>
                
<apex:pageBlockTable value="{!Office__c.Office_Commissions__r}" var="OffCom1" >
                     <apex:outputLabel value="End Date" />
                     <apex:inputField value="{!OffCom1.Search_End_Date__c}"/>
                 </apex:pageBlockTable>                 
<apex:commandButton value="Disply" action="{!displaingTable}" reRender="tableId"/>
</apex:pageBlockSection>


<apex:pageBlockSection >
<apex:pageBlockTable value="{!Office__c.Office_Commissions__r}" id="tableId" var="OffCom" >
<apex:column headerValue="Office Commissions" >
<apex:commandLink rerender="OffComdetails">  {!OffCom.Name}
<apex:param name="OffComid" value="{!OffCom.id}"/>
</apex:commandLink>
</apex:column>
<apex:column value="{!OffCom.Upfront_Bonus_Average__c}"/>
<apex:column value="{!OffCom.Ongoing_Bonus_Average__c}"/>

<apex:column value="{!OffCom.Commission_Period_Start__c}"/>
<apex:column value="{!OffCom.Commission_Period_End__c}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>

</apex:form>
</apex:pageBlock>
<apex:outputPanel id="OffComdetails">
<apex:detail subject="{!$CurrentPage.parameters.OffComid}" relatedList="False" inlineEdit="True" title="false"/>
</apex:outputPanel>

</apex:page>

Extension:

public with sharing class SearchExt{




    public Office_Commission__c OffCom {get;set;}
    public list<Office_Commission__c> listOffCom {get;set;}
    public SearchExt(ApexPages.StandardController controller) {
   
    this.OffCom = (Office_Commission__c)controller.getRecord();
    }
    public Date startDate1;
    public Date endDat1;
    public SearchExt() {
      
        OffCom = new Office_Commission__c();
    }
     public PageRefeRence  displaingTable(){
        startDate1 = OffCom.Search_Start_Date__c;
        endDat1 = OffCom.Search_End_Date__c;
        listOffCom= [SELECT id, Name
                          FROM Office_Commission__c
                          WHERE Commission_Period_Start__c >=: startDate1 AND Commission_Period_End__c<= :endDat1+1];
        return null;
     }
   
}

Any help would be much appreciated. Whenever I try and load the page I get the error:

System.TypeException: Invalid conversion from runtime type SOBJECT:Office__c to SOBJECT:Office_Commission__c
Class.SearchExt.<init>: line 10, column 1


NishBNishB
Hi,
    In the page the standard Controller is Office__c but in the contructor SearchExt(ApexPages.StandardController controller)  you are trying to fetch the records of Office_Commission__c  i. e this.OffCom = (Office_Commission__c)controller.getRecord() so it should be  (Office__c)controller.getRecord()
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Yes, I know. I am trying to filter a pageBlockTable of the records associated with a master Object, as in its child records.
NishBNishB
So I believe the run time error is because of that 
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Is it possible to rectify this, yet still get the records through the office__c master object?
NishBNishB
You are trying to display child records associated with a master record filtered by dates did I get it correct?
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Yes, exactly that. Thank you so much if you can think of anyways to get this working.
NishBNishB
Office__c -  parent , Office_Commission__c - child

public List<Office_Commission__c> offCommLst{get;set;}
In the contructor  do this  
Id parentId= controller.getId()  

and the query in the contructor 

 offCommLst = [SELECT id, Name
                          FROM Office_Commission__c
                          WHERE Commission_Period_Start__c >=: startDate1 AND Commission_Period_End__c<= :endDat1+1 AND Office__c=:parentId];

Use the offCommLst in the VF page

Standard Controller methods:

https://www.salesforce.com/us/developer/docs/pages/Content/apex_ApexPages_StandardController_methods.htm

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Hey Nisha, Thank you for the reply. I have tried to use your code, but I am a beginner at Apex still. This is my attemt. It is returning an error:
Error: Compile Error: Variable does not exist: controller at line 16 column 18


Class:
public with sharing class SearchExt{




    public Office_Commission__c OffCom {get;set;}
    public List<Office_Commission__c> offCommLst{get;set;}
   
    public SearchExt(ApexPages.StandardController controller) {
   
    this.OffCom = (Office_Commission__c)controller.getRecord();
   
    }
    public Date startDate1;
    public Date endDat1;
    Id parentId= controller.getId();
    public SearchExt() {
      
        OffCom = new Office_Commission__c();
    }
     public PageRefeRence  displaingTable(){
        startDate1 = OffCom.Search_Start_Date__c;
        endDat1 = OffCom.Search_End_Date__c;
        offCommLst = [SELECT id, Name
                          FROM Office_Commission__c
                          WHERE Commission_Period_Start__c >=: startDate1 AND Commission_Period_End__c<= :endDat1+1 AND Office__c=:parentId];
        return null;
     }
   
}
NishBNishB
What I meant was this...


public List<Office_Commission__c> offCommLst {get;set;}

public SearchExt(ApexPages.StandardController controller) {
  
    //this.OffCom = (Office_Commission__c)controller.getRecord();

   Id parentId = (Office_Commission__c)controller.getId();    /* getId() is a standard controller instance method...please go through the link that was shared in the prev post*/

offCommLst = [SELECT id, Name
                          FROM Office_Commission__c
                          WHERE Commission_Period_Start__c >=: startDate1 AND Commission_Period_End__c<= :endDat1+1 AND Office__c=:parentId];
  
    }

How are the startDate1 and endDat1 dates values got are they user input values and are you displaying the filtered child records only when the Diplay button is clicked?



Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
I was thinking along the lines of ajax re-rendering the records in a pageblocktable when the two dates are input by the user on the page. Once again thank you for your help. :)