0

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.

2

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.
| improve this answer | |
  • Everything is perfect...Am not getting the apex message..if field has no value – Paps Mar 16 '17 at 6:57
  • I'd recommend not calling the variable Account. It's a bad practice to get in to. – sfdcfox Mar 16 '17 at 7:05
  • @sfdcfox good point. – martin Mar 16 '17 at 7:06
  • Still not getting the Apex Message...."No value found" – Paps Mar 16 '17 at 7:10
  • no problem‌​​​​​​​​​‌​​​​​​​​​‌​​​​​​​​​‌​​​​​​​​​‌​​​​​​​​​ – martin Mar 16 '17 at 8:05
1

No Account is sobject name never use the sobject name and reserved keyword for class name or other variables.

Check this Reserved keywords

If you just changed the class name

public with sharing class AccountCustomCtrl{

  public AccountCustomCtrl(){
   // code here
  }

  public PageReference siteValue(){
   //code here
  }
}

Now in page use this custom controller class like below example

<apex:page Controller="AccountCustomCtrl"  showHeader="false" >
   <!--code here-->

   <apex:actionPoller interval="5" action="{!siteValue}" 
         reRender="gen" status="pollerStatus"/>
</apex:page>

Now this will call you method siteValue


Updates

If you need the standard controller then use standardController with extension

so your page code will be like

<apex:page  standardController="Account" extensions="AccountCustomCtrl" showHeader="false">
   <!--code here-->

   <apex:actionPoller interval="5" action="{!siteValue}" 
         reRender="gen" status="pollerStatus"/>
</apex:page>
| improve this answer | |
  • I want this vf page as Inline vf page...So SObject name is compulsory ,right? – Paps Mar 16 '17 at 6:44
  • can you tell what you are trying to achieve? – night crawler Mar 16 '17 at 6:45
  • @Paps check the above updates. – Ratan Paul Mar 16 '17 at 6:46
  • Am just displaying a field value in Account through inline vf .... If that field has no value it should show..."No value found"..... – Paps Mar 16 '17 at 6:52

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.