Search code examples
javascriptsalesforceapex-codevisualforce

Call extension method from Javascript Visualforce


I've been trying to call an extension method from my visualforce page.

<apex:page title="Title" standardController="Account" showChat="true" tabStyle="Account" standardStylesheets="true"  showHeader="true" extensions="AccountExtension">

<script type="text/javascript">
Sfdc.onReady(function() {
        if({!isTrue}){
            // Do Something
        }
});
</script>

My method in my extension:

public with sharing class AccountExtension {
    public boolean isTrue(){
        return true;
    }
}

When I try to save it, I'm getting the message: Error: Unknown property 'AccountStandardController.isTrue'

Any idea?


Solution

  • The first option as it was mentioned Lex in comment is renaming method to getIsTrue() - it will be a getter for property isTrue, which will be under the hood for you.

    The another option is creation apex property in the following manner:

     public with sharing class AccountExtension {
    
         public AccountExtension(ApexPages.StandardController controller) {
         }
    
         public Boolean isTrue {
             get{
                 isTrue = false;
                 //implement logic
                 return isTrue;
             }
             set{}
         }
     }