You need to sign in to do that
Don't have an account?
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
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
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()
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
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;
}
}
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?