0

I'm trying to access helper method from Visualforce page directly and I'm not sure if this possible and I'm getting the following error below and with the code I have.

Error

Error: Unknown property 'Invoice_Statement__cStandardController.HelperClass'

Visualforce page:

<apex:selectList size="1" value="{!Invoice_Statement__c.Status__c}">
      <apex:selectOptions value="{!HelperClass.loadNoNoneForPicklist}"/>
</apex:selectList>

Helper class:

public class HelperClass
{

    public static Invoice_Statement__c invoiceStatement {get;set;}
    public static List<SelectOption> statusOptions {get;set;}

    public static loadNoNoneForPicklist() 
    {
        invoiceStatement = new Invoice_Statement__c();        
        statusOptions = new List<SelectOption>();

        // Use DescribeFieldResult object to retrieve status field.
        Schema.DescribeFieldResult statusFieldDescription = Invoice_Statement__c.Status__c.getDescribe();

        // For each picklist value, create a new select option
        for (Schema.Picklistentry picklistEntry: statusFieldDescription.getPicklistValues())
        {
            statusOptions.add(new SelectOption(pickListEntry.getValue(),pickListEntry.getLabel()));

            // obtain and assign default value
            if (picklistEntry.defaultValue)
             {
                invoiceStatement.Status__c = pickListEntry.getValue();
            }  
        } 
        return statusOptions;    
    }
}
  • That's not even valid syntax. You need to declare a return type for loadNoNoneForPicklist. – Adrian Larson Aug 17 '16 at 17:01
  • its typo copy/paste missed that – Nick Kahn Aug 17 '16 at 18:16
2

You have to turn the class into an extension or controller, or you can reference it via a method in your main controller.


As Controller

Page

<apex:page controller="HelperClass" >

Controller

public class HelperClass {
    public HelperClass() {
    }
    public static SelectOption[] loadNoNoneForPicklist() {
        // build values here
    }
    // rest of code here
}

Extension With StandardController

Page

<apex:page standardController="SObject" extensions="HelperClass">

Controller

public class HelperClass {
    public HelperClass(ApexPages.StandardController controller) {
    }
    public static SelectOption[] loadNoNoneForPicklist() {
        // build values here
    }
    // rest of code here
}

Extension With Custom Controller

Page

<apex:page controller="MyCustomController" extensions="HelperClass">

Controller

public class HelperClass {
    public HelperClass(MyCustomController controller) {
    }
    public static SelectOption[] loadNoNoneForPicklist() {
        // build values here
    }
    // rest of code here
}

Accessing From Custom Controller

Controller

public class PrimaryController {
    public SelectOption[] loadNoNoneForPicklist() {
        return HelperClass.loadNoNoneForPicklist();
    }
    // rest of code here
}
| improve this answer | |
  • @AdrianLarson Fair enough. I'll clean it up. – sfdcfox Aug 17 '16 at 17:09
2

You need to expose a property on an extension. Something like:

public with sharing class MyExtension
{
    public List<SelectOption> options { get; private set; }
    public MyExtension(ApexPages.StandardController controller)
    {
        options = HelperClass.loadNoNoneForPicklist();
    }
}

But why even make it static? This functionality does not seem highly reusable. It may be better to just put the logic in your constructor (or getter) and be done with it. Make sure you add this class to your extensions property on the page.


Your markup should look something like:

<apex:page standardController="Invoice_Statement__c" extensions="MyExtension">
    <!--intermediate markup-->
    <apex:selectList value="{!Invoice_Statement__c.Status__c}" />
        <apex:selectOptions value="{!options}" />
    </apex:selectList>
</apex:page>

Related Reading:

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.