You need to sign in to do that
Don't have an account?

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'".
Where as the following code is saved and executed successfully. though account has only get property.:
Can you please help in understanding this different behavior.
Thanks,
Naveen.
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.
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.
May I request you please refer the below link for reference.
- https://success.salesforce.com/answers?id=90630000000Cei5AAC
- https://developer.salesforce.com/forums/?id=906F000000092DMIAY
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
Public String name{get;set;}