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
MG ConsultingMG Consulting 

Custom Component Attribute is Null in its Controller

Hi,

I can't access a custom component's attribute value from it's controller's constructor. Below is a simple test case that demostrates the problem:
 

<apex:page showHeader="false"> <c:Scratch testAttr="Test Value"/></apex:page>

 

<apex:component controller="Scratch"> <apex:attribute name="testAttr" description="test desc" type="String" assignTo="{!TestProp}"/> <div> <apex:outputText value="testAttr: {!testAttr}"/> </div> <div> <apex:outputText value="TestProp: {!TestProp}"/> </div></apex:component>

 

 

public class Scratch { public Scratch() { System.debug('TEST_PROP: ' + TestProp); } public String TestProp { get; set; } }

 

As you can see the outputText for both testAttr & TestProp correctly dislpay "Test Value" but the System.debug message shows TestProp's value as being null.
 
Anyone know what's going on here?
 
Thanks a lot,
Mike
jwetzlerjwetzler

your controller has to be constructed before the setter on your attribute can be called, right?  That's how object oriented programming works.

 

So that's the wrong place to put your debug statement.

MG ConsultingMG Consulting

Oh man, yeah, your absolutely right, sorry for the brain fart. I'll have to see if I can't find another place to do the piece of the component's initialization which is dependent on the incoming attribute values.

 

Anyway, thanks for the quick reply.

 

While I have your attention maybe you could give this one a stab:

 

Any ideas's why two instances of a custom component's controller would be created for just one instance of the custom component?

 

http://community.salesforce.com/sforce/board/message?board.id=Visualforce&message.id=13068 

   

 

Thanks again,

Mike

jwetzlerjwetzler
Without an easily reproducible case I could copy into my own test org there's not much I can tell you.  Doesn't sound like something we would be doing....
Message Edited by jwetzler on 05-27-2009 01:45 PM
MG ConsultingMG Consulting

Fixed formatting, sorry, Chrome doesn't seem to like line breaks with cut and paste, back to Firefox for this forum.

 

Hi Jill, 

 

I understand completely, so I just worked one out, sorry for the length, and thanks again for your help.

 

Scratch.page 

 

<apex:page showHeader="false">
<c:Scratch >
<c:Scratch2 contactOne="{!contactOne}" contactTwo="{!contactTwo}"/>
</c:Scratch>
</apex:page>

 

Scratch.component 

<apex:component controller="Scratch" allowDML="true">
<apex:outputText value="{!ContactOne.Name}" escape="false"/>
<div>
<apex:outputPanel rendered="{!HasFatalMessages}" layout="none">
<div>
<apex:messages layout="table"/>
</div>
</apex:outputPanel>
<apex:outputPanel rendered="{!!HasFatalMessages}" layout="none">
<apex:outputPanel rendered="{!!ISNULL(ContactTwo)}" layout="block">
<div>
<apex:outputField value="{!ContactTwo.FirstName}"/>
<div>
<apex:form >
<apex:commandLink action="{!save}" value="Save"/>
</apex:form>
</div>
</div>
</apex:outputPanel>
<apex:componentBody >
<apex:variable var="contactOne" value="{!ContactOne}"/>
<apex:variable var="contactTwo" value="{!ContactTwo}"/>
</apex:componentBody>
</apex:outputPanel>
</div>
<apex:outputText value="{!ContactOne.Name}" escape="false"/>
</apex:component>

 

Scratch2.component  

<apex:component >
<apex:attribute name="contactOne" description="Contact One" type="Contact" required="true"/>
<apex:attribute name="contactTwo" description="Contact Two" type="Contact" required="true"/>
</apex:component>

 

 

 

 Scratch.cls

public class Scratch {

public Scratch() {
System.debug('SCRATCH_CONSTRUCTOR');
}

public Boolean getHasFatalMessages() {
return ApexPages.hasMessages(ApexPages.Severity.FATAL);
}

private final Contact contactOne;
public Contact getContactOne() {
return contactOne;
}

private Contact contactTwo;
public Contact getContactTwo() {
return contactTwo;
}

public PageReference save() {
return null;
}
}

 

You should see SCRATCH_CONSTRUCTOR appear twice in the debug logs, which doesn't seem to make sense.

 

Hopefully this isn't another one of my brain farts :)

 

Regard,

Mike 

 

Message Edited by MG Consulting on 05-27-2009 05:49 PM
ShambolicRuseShambolicRuse

I'm seeing this issue all over the place on the discussion boards? Why hasn't someone addressed this?