[SalesForce] Getting this error Error: Unknown method ‘AccountStandardController.siteValue()’

Visualforce page:

<apex:page standardController="Account"  showHeader="false" >
   <apex:form >
        <apex:pageBlock title="Account Detail">
       <apex:pageblockSection title="Site Field Value" columns="1" collapsible="false">
        <apex:inputText value="{! Account.Site}"  id="gen"/>
         <apex:actionPoller interval="5" action="{!siteValue}" reRender="gen" status="pollerStatus"/>
         <apex:actionstatus startText="Updating Site values...." id="pollerStatus" ></apex:actionstatus>
            <apex:pageBlockSectionItem >
   </apex:pageBlockSectionItem> 
</apex:pageblockSection> 
            <apex:pageBlock>  
             <apex:pageMessages ></apex:pageMessages>  
     </apex:pageBlock>
  </apex:pageBlock>

=====controller class======

public with sharing class Account{
 public List<Schema.Account> accountList = new List<Schema.Account>();


public Account(){

  accountList = [select Site from Account];

}          

public PageReference siteValue(){

     if(Schema.Account.Site == null){
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'No value found'));

       }

         return null;
        }      
    }

Thank you in advance.

Best Answer

In order to access methods of a custom apex class in a visualforce page, you should set that class as either the controller, or as one of the extensions on the page.

<apex:page standardController="Account" extensions="AccountPageController" showHeader="false" >
⋮
<apex:actionPoller interval="5" action="{!siteValue}" reRender="gen,messages" status="pollerStatus"/>
⋮
<apex:pageMessages id="messages"></apex:pageMessages>  

I should also add that in order for a class to be used as an extension, it must have a constructor that takes the StandardController as an argument. Also, you can't use Account as the name of your class since that apex class name already refers to the standard sObject, so your controller should be something like:

public with sharing class AccountPageController{
  public List<Schema.Account> accountList = new List<Schema.Account>();
  public Account a {get; set;}
  public AccountPageController(ApexPages.StandardController controller){
    accountList = [select Site from Account];
    a = (Account) controller.getRecord();
  }          

  public PageReference siteValue(){
    if(a.Site == null || a.Site.trim() == ''){
      ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'No value found'));
    }
    return null;
  }      
}

Also, Schema.Account.Site gets the describe information for the Site field on Account. To get the value of the Site field on the Account that is referenced on the page, you should save a reference to the account from the controller when your apex class is constructed, and use that to determine the current value of Site field.

Two more things, your error message still isn't showing up because:

  1. The pageMessage component isn't getting rerendered after the action.
  2. The Site field might be the white space instead of null, and you should test for that as well.
Related Topic