function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
prabhavathi sharmaprabhavathi sharma 

how to call the apex method in visualforce page

how to call the apex method in visualforce page. where I did mistake tell me. I am getting this Error: Unknown property 'Example.name
I wrote this code 

Apex code
public class Example 
{
    
    pubic string name;
    
    public string getName()
    {
        return name;
    }
    public void setName(string name)
    {
        this.name=name;
    }
    public pagereference show()
    {
        name='This is my name'+name;
        return null;
    }
}

Visualforce code

<apex:page controller="Example">
 <apex:form>
     <apex:outputlabel>Enter Name</apex:outputlabel>
     <apex:inputtext value="{!name}"/>
     <apex:commandButton value="click" rerender="one" action="{!show}"/>
     <apex:outputLabel id="one">{!name}</apex:outputLabel>     
 </apex:form>
</apex:page>


 
sfdcMonkey.comsfdcMonkey.com
hi prabhavathi sharma 
use below code , i think name is a reserved keyword for use in public class varibale.
public class Example {
    
    public string sName;
    
    public string getName(){
        return sName;
    }
    public void setName(string name){
        this.sName = name;
    }
    public pagereference show(){
       sName = 'This is my name '+ sName;
        return null;
    }
}
thanks
mark it best answer if my answer helps you so it make proper solution for others

 
sfdcMonkey.comsfdcMonkey.com
and output is
User-added image
Amit Chaudhary 8Amit Chaudhary 8
Hi Prabhavathi sharma ,

Name is not Keyword . You just need to add getter and setter for Name Field.
You can try below code
public class Example 
{
    public String name{get;set;}    
    
    public pagereference show()
    {
        name='This is my name'+name;
        return null;
    }
}

Page like below
<apex:page controller="Example">
 <apex:form >
     <apex:outputlabel >Enter Name</apex:outputlabel>
     <apex:inputtext value="{!name}"/>
     <apex:commandButton value="click" rerender="one" action="{!show}"/>
     <apex:outputLabel id="one">{!name}</apex:outputLabel>     
 </apex:form>
</apex:page>

I tested the same code in my developer org which is working fine
User-added image

Please check below post for Getter Setter
1) https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_methods.htm
2) http://www.forcetree.com/2009/07/getter-and-setter-methods-what-are-they.html
3) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_properties.htm


Let us know if you need more help.