0

I am a beginner in the salesforce world. I have my page:

<apex:page controller="opportunityList2Con">
    <apex:pageBlock >
        <apex:pageBlockTable value="{!employees}" var="o">
            <apex:column value="{!o.Name}"/>
            <apex:column value="{!o.Email__c}"/>
            <apex:column value="{!AnnualSalary}"/>
            </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

I have my Apex class called opportunityList2Con and there I have a method called getAnnualSalary which looks like that:

  Integer getAnnualSalary(Employee__c emp){

    return 123;
}

Unfortunately no such column like annual salary is printed on the screen. What do I wrong?

Thanks for your answers.

2
  • Do you have a space between annual and salary , can you paste your complete class
    – Rao
    Oct 6, 2015 at 20:53
  • 1
    There is no mechanism in Visualforce to pass arguments to methods. You could invoke the getter if its signature was Integer getAnnualSalary().
    – Keith C
    Oct 6, 2015 at 21:02

1 Answer 1

2

Several things...

  1. your method must be public

  2. methods called from VF cannot have parameters

  3. methods called from VF can only return PageReferences (other than get methods, which are really just treated as properties)

Visualforce uses a MVC framework. What you really need to do is to create a model to communicate between your View (VF page) and Controller (Apex Class).

In your Controller, create an inner class:

public class MyController{
    public List<Employee> employees {get; set;}

    public MyController(){
        employees = new List<Employee>();
        // add employees
    }

    //inner employee class
    public class Employee{
      public MyObject__c obj {get; set;}
      public Employee(MyObject__c obj){
         this.obj = obj;
      }

      public Integer getAnnualSalary(){
          return 123;
      }  
    }
}

In your apex page you can treat the AnnualSalary as a property:

<apex:page controller="opportunityList2Con">
    <apex:pageBlock >
        <apex:pageBlockTable value="{!employees}" var="o">
            <apex:column value="{!o.obj.Name}"/>
            <apex:column value="{!o.obj.Email__c}"/>
            <apex:column value="{!o.AnnualSalary}"/>
            </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

You must log in to answer this question.

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