3

I have an attribute within a custom component, but it doesn't appear to be calling any get/set methods regardless of how I format either the attribute (specifying assignTo) or writing the get/set code itself. I've double checked this with the Debug Logs and it simply does not get called. This is what I have (I've simplified it as much as possible, the actual final implementation won't have static content

Apex Page:

<apex:page standardController="Invoice__c"  showHeader="false" renderas="pdf">
    <c:Invoice_PDF_HTML_Content invoiceId="a0pO0000000lBcP" />
</apex:page>

Apex Component:

<apex:component access="global" controller="Invoice_Quote_Controller" >

    <apex:attribute access="global" name="invoiceId" description="Invoice ID" type="String" />

    <!-- HTML-->
</apex:component>

Controller:

global without sharing class Invoice_Quote_Controller {
    private String invoiceId = null;

    public String getInvoiceID(){
        system.debug(invoiceId);
        return invoiceId;
    }

    public void setInvoiceID(String InvoiceId){

     system.debug(InvoiceId);
     system.debug(invoiceId);
     if(this.invoiceId==null){

        this.invoiceId = InvoiceId;
        // Constructor code
    }
}

//Blank Constructor
global Invoice_Quote_Controller() {

} 
}

I've got the getter and the setter for invoice_id inside Invoice_Quote_Controller. It just never gets called, what am I doing wrong? (Is it something to do with the global scope?)

11
  • Have you tried another scope? Seems odd that it wouldn't call the setter after instantiation of the controller.
    – Matt Lacey
    Nov 6, 2013 at 0:22
  • Just tried making it public and it still doesn't appear to work :/
    – Mattisdada
    Nov 6, 2013 at 0:42
  • Can you post the controller too?
    – Matt Lacey
    Nov 6, 2013 at 0:44
  • Sure, give me a few mins to edit. EDIT: Edited the question to include the controller
    – Mattisdada
    Nov 6, 2013 at 0:44
  • 1
    I think the problem is your assignTo="{!invoice_id}" does not match your get/set methods in your controller have you tried assignTo="{!InvoiceID}" Nov 6, 2013 at 0:55

1 Answer 1

4

In order to assign the values of Visualforce Components to variables in your controllers you need to apply the assignTo attribute, as per the docs this is described as referencing...

A setter method that assigns the value of this attribute to a class variable in the associated custom component controller. If this attribute is used, getter and setter methods, or a property with get and set values, must be defined.

The docs also show this example which helps illustrate its use...

Similar to standard Visualforce pages, custom components can be associated with a controller written in Apex. This association is made by setting the controller attribute on the component to your custom controller. You can use the controller to perform additional logic before returning the component's markup to the associated page.

public class myComponentController {

  public String controllerValue;

  public void setControllerValue (String s) {
    controllerValue = s.toUpperCase();
  }

  public String getControllerValue() {
    return controllerValue;
  } 
}

The component markup looks like this...

<apex:component controller="myComponentController">
  <apex:attribute name="componentValue" description="Attribute on the component."
                  type="String" required="required" assignTo="{!controllerValue}"/>
    <apex:pageBlock title="My Custom Component">
      <p>
        <code>componentValue</code> is "{!componentValue}"
        <br/>
        <code>controllerValue</code> is "{!controllerValue}"
      </p>
    </apex:pageBlock>
    Notice that the controllerValue has been upper cased using an Apex method.
</apex:component>

You can also utilise the short hand notation for properties, which can also include code to execute.

public class myComponentController {

  public String controllerValue {get; set; }

}

Finally keep in mind the order the setter methods are called is none deterministic, so don't have properties with dependencies. I know this is the only way if you want some kind of construction logic, just handle with care.

Glad to be of help! :-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .