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
naveen reddy 19naveen reddy 19 

Apex Getter Setter Differrent behaviour for Account and String types

Hi,

When I'm working on getters and setters in VF and Apex. I came across differrent behaviour when I'm referring the account and string types.

The following code is giving this error "Error: Read only property 'MyControllerVF.name'".
 
public class MyControllerVF {
    private string name;
 
    public MyControllerVF() {
                          
        name='Test';
  }

    public string getName()
   {
     return name;
    
   }
 public PageReference save() 
    {      
      system.debug(name);
       return null;
   }
  }
 
<apex:page controller="MyControllerVF" tabStyle="Account">
    <apex:form >
        <apex:pageBlock title="Congratulations {!$User.FirstName}">
           <apex:inputtext value="{!name}"/>
          <apex:commandButton action="{!save}" value="save"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Where as the following code is saved and executed successfully. though account has only get property.:
 
public class MyController {
 
    private Account account;
 
    public MyController() {
        account = [SELECT Id, Name, Site FROM Account
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }
 
    public Account getAccount() {
        return account;
    }
 
    public PageReference save() {
        update account;
        return null;
    }
}
 
<apex:page controller="myController" tabStyle="Account">
    <apex:form>
        <apex:pageBlock title="Congratulations {!$User.FirstName}">
            You belong to Account Name: <apex:inputText value="{!account.name}"/>
            <apex:commandButton action="{!save}" value="save"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>


Can you please help in understanding this different behavior.

Thanks,
Naveen.
Prakash NawalePrakash Nawale
Hi Naveen,

In controller you defiend
 private string name;

In VF page you are setting value to name property.
   <apex:inputtext value="{!name}"/>

You have to create setter proptery to  resove this.
 
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Naveen Reddy,
May I request you please refer the below link for reference. hope it will be helpful.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar
 
Varun SinghVarun Singh
You have to change  just your controller  code;set;

Public String name{get;set;}
 
public class MyControllerVF {
    public string name {get;set;} 
    public MyControllerVF() {
                          
        name='Test';
  }

    public string getName()
   {
     return name;
    
   }
 public PageReference save() 
    {      
      system.debug(name);
       return null;
   }
  }