You need to sign in to do that
Don't have an account?
Using <apex:attribute> from a Custom Component in the Controller's Query
I have a visualforce page that uses a custom component. I want the component's controller to be aware of a variable that should be used in the the WHERE clause of the controller's query. My code is below:
<!-- visualforce page -->
<apex:page controller="pageController" standardStylesheets="true" tabStyle="SObject__c">
<apex:repeat value="{!categories}" var="c">
<c:customComponent cName="{!c.Name}" cRef="{!c.Id}"></c:customComponent>
</apex:repeat>
</apex:page>
<!-- custom component -->
<apex:component controller="componentController">
<apex:attribute name="cName" description="desc..." type="String" required="true"></apex:attribute>
<apex:attribute name="cRef" description="String to use for controller class." type="String" required="true" assignTo="{!value}"></apex:attribute>
<div class="customStyle">
<apex:dataTable id="linkList" value="{!links}" var="link">
<apex:column>
<apex:facet name="header">Field 1</apex:facet>
<apex:outputText value="{!link.Field_1__c}"></apex:outputText>
</apex:column>
<apex:column>
<apex:facet name="header">Field 2</apex:facet>
<apex:outputText value="{!link.Field_2__c}"></apex:outputText>
</apex:column>
</apex:dataTable>
</div>
</apex:component>
<!-- component controller -->
public with sharing class componentController {
public SObject__c[] links = new List<SObject__c>();
String value;
public String getValue() {
return value;
}
public void setValue(String s) {
value = s;
}
public componentController() {
try {
links = Database.query('SELECT Field_1__c, Field_2__c FROM SObject__c WHERE Field_3__c=:value');
} catch (Exception e) {
System.Debug('Exception: '+e);
}
}
public SObject__c[] getLinks() {
return links;
}
}
Basically, the "cRef" attribute from the component does get assigned to the "value" variable in the componentController but "value" is actually NULL when the query inside the componentController is executed.
Can anyone point out what my code is missing in order to use the "value" from the component in the query?
Any help is greatly appreciated,
-greg
This is just an educated guess, but assignTo is going to call setValue() on your controller, and it can't call setValue() until it's actually constructed your class. So I would do:
public SObject__c links = null;
public SObject__c[] getLinks() {
if (links == null) {
links = //do query here
}
return links; }
That way you give setValue() a chance to get called before you do the query.
All Answers
This is just an educated guess, but assignTo is going to call setValue() on your controller, and it can't call setValue() until it's actually constructed your class. So I would do:
public SObject__c links = null;
public SObject__c[] getLinks() {
if (links == null) {
links = //do query here
}
return links; }
That way you give setValue() a chance to get called before you do the query.
Thanks Jill. You are correct. When I moved my query it fixed the problem.
-greg