2

VF Code

<apex:page controller="Buffer">
    <apex:form >
        <apex:pageBlock >
             <apex:pageBlockButtons location="top">
                <apex:commandButton value="Save" action="{!SaveMethod}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!c}" var="t">
                <apex:column headerValue="Name">
                    <apex:inputText value="{!t.Name}"/>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Code

public class Buffer 
{
    public List<Contact> c {get;set;}
    public String ids{set;get;}
    
    public Buffer()
    {
        ids=ApexPages.currentPage().getParameters().get('id');
        c = new List<Contact>([select Name from Contact where id=:ids]);
    }
    
     public PageReference SaveMethod()
    {
        update c;
        return new PageReference('/'+ids);
    }    
}

enter image description here

I am trying to change the name of the contact and click Save, but the name of the contact remains the same and doesn't get changed.

Could anyone please let me know what exact change can be done to the code so that I can edit the name of the contact and save it successfully from the vf page.

1
  • 1
    P.S. c = new List<Contact>([select Name from Contact where id=:ids]); is redundant, as the result of a query is already a List. c = [select Name from Contact where id=:ids]; works just fine.
    – sfdcfox
    Sep 6, 2022 at 18:37

1 Answer 1

3

You can't edit the Name field; it is a compound field that uses FirstName, MiddleName (if enabled), Last Name, and Suffix. You need to edit the individual fields, instead.

<apex:column headerValue="First Name">
  <apex:inputField value="{!t.FirstName}" />
</apex:column>
<apex:column headerValue="Last Name">
  <apex:inputField value="{!t.LastName}" />
</apex:column>

Also, note that you need to query a field in order to use it:

c = [select FirstName,LastName from Contact where id=:ids];
6
  • I was going to say, even if he could edit the Name field, wouldn't he have to use inputField instead of inputText? Or does that component also work to edit directly sObject fields? Sep 6, 2022 at 18:37
  • 1
    @joacoiudica I didn't notice that, but ultimately, it doesn't matter. apex:inputText works just as well as apex:inputField if you know the field is a text field, except the former does not respect field level security (so, if they'd used the latter, the field would have been read only, which should have been a clue).
    – sfdcfox
    Sep 6, 2022 at 18:38
  • 1
    @joacoiudica actually, in retrospect, they may have tried to use apex:inputField first, and it wasn't showing an input element, so they then tried apex:inputText. I could imagine a younger me doing that until I realized what's wrong. In fact, I probably did do that at least once while I was learning Visualforce.
    – sfdcfox
    Sep 6, 2022 at 18:42
  • @sfdcfox I did try the snippet of code provided by you, but I am getting error as below: SObject row was retrieved via SOQL without querying the requested field: Contact.FirstName
    – Ali Baba
    Sep 6, 2022 at 18:50
  • @AliBaba Yes, I just realized you named the var t. I've edited this answer to reflect that. Also, you need to add those fields to the query in order to use them.
    – sfdcfox
    Sep 6, 2022 at 18:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .