0

I have created simple vf component with its own controller but apparently you can not have vf component controller to call any soql and I have tried the following code below and does not show any count in the vf component and is that even possible to have something below? if yes then what i'm doing wrong here?

Class:

public class myComponentController 
{        
  public List<Account> myAccounts {get;set};

  public String myComponentController() {
     myAccounts = [SELECT Name FROM Account LIMIT 10];
  } 
}

Component Page:

<apex:component controller="myComponentController">       
    <apex:pageBlock title="My Custom Component">
      <p> 
        <code>Account</code> is "{!myAccounts.size}"
      </p>
    </apex:pageBlock>        
</apex:component>

If that above is not possible then I know the other way that you send through <apex:attribute.... but i was curious to know why the above option is not available.

1 Answer 1

0

Your code is not syntactically correct.

Your semicolon in your get and set must be within the {}. A component instantiation also does not return a value. Please see below:

public class myComponentController {        
  public List<Account> myAccounts {get;set;}

  public myComponentController() {
     myAccounts = [SELECT Name FROM Account LIMIT 10];
  } 
}

To display a value from the component, you can use an apex page, where your component name is "AccountComponent":

<apex:page >
 <c:AccountComponent />
</apex:page>

This will then return the following:

Display Value of Component

What you have above wouldn't necessarily need to be component unless you plan on parameterizing and using the component across multiple pages. Please see the documentation here on apex components for more information: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_component.htm

1
  • the simicolon was a typo; my code is working as expected now... i have no clue what was the issue.
    – Nick
    Dec 13, 2016 at 2:18

You must log in to answer this question.

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