1

In a visualforce page I have an input for the user to enter his phone number :

<apex:input value="{!newPhone}" type="tel" StyleClass="small" html-placeholder="Téléphone *" required="true"/>

(And I have a save button that call the changeInfo() of the controller)

And I want to save that number in salesforce, so with the controller I do that :

public Id accountId{get;set;}
public String newPhone{get; set;}

public PageReference changeInfo(){
    Account account = [SELECT Name,Phone FROM Account WHERE Id=:accountId LIMIT 1];

    account.Phone = newPhone;
    try{
        update account;
    }
    catch(DMLException e){
        error = e.getMessage();
    }
    PageReference pageRef = new PageReference('/apex/myPage?AccountId=' + accountId);
    pageRef.setRedirect(true);
    return pageRef;
}

But When I click the save button, it redirect me to the right page, but the value is not saved in salesforce. Is it possible that my problem comes from the data type of newPhone ? Anyway any help is welcome, thank you !

5
  • 2
    Its possible you are generating an exception and not seeing it, as you'll always refresh the page after you try the update, regardless if there was a DMLException or not. Commented Sep 21, 2016 at 13:23
  • Yeah you're right I got that error now : "INVALID_FIELD_FOR_INSERT_UPDATE, Account: bad field names on insert/update call: Name: [Name]"
    – dou
    Commented Sep 21, 2016 at 13:44
  • 2
    It seems like your code doesn't use that Name field. Does it save when you remove the name from the soql? Commented Sep 21, 2016 at 13:45
  • Yes when I remove it it works and the value is saved. I've put it because I'll need to have acces to the name (firstName, lastName and salutation) but maybe I should put FrstName in the query
    – dou
    Commented Sep 21, 2016 at 13:49
  • If you're using person accounts, I think the Name field turns into an complex field, which is basically just an object, as opposed to a name, so you'll want to grab exact fields- FirstName instead of Name, or BillingStreet and not BillingAddress. Commented Sep 21, 2016 at 13:59

1 Answer 1

2

Its possible that you are throwing, then catching, a DMLException, without fully handling it.

Consider the following:

<apex:form id="someForm">
    <apex:pageMessages />
    <apex:input value="{!newPhone}" type="tel" StyleClass="small" html-placeholder="Téléphone *" required="true" />
    <apex:commandButton action="{!changeInfo}" value="Update Phone" reRender="someForm" />
</apex:form>

This visualforce snippet assumes that you have set up your page to properly handle apexPageMessage's being added to the page, with a rerender attribute and a apex:pageMessages tag.

public Id accountId { get; set; }
public String newPhone { get; set; }

public PageReference changeInfo(){
    Account account = [SELECT Name, Phone FROM Account WHERE Id=:accountId LIMIT 1];

    account.Phone = newPhone;

    try {
        // update account; 
        throw new DMLException(); 
    } catch(DMLException e) { // Catches Exception, and logs error 
        error = e.getMessage(); // I assume your code does something with this error, used to handle failure cases, such as show a message 
    }

    // Any errors/messages are overridden because regardless of the error case, you switch the page 

    PageReference pageRef = new PageReference('/apex/myPage?AccountId=' + accountId);
    pageRef.setRedirect(true);

    return pageRef; 
} 

Id write something more like this, which doesn't redirect when an exception is thrown.

public Id accountId { get; set; }
public String newPhone { get; set; }

public PageReference changeInfo(){
    Account account = [SELECT Name, Phone FROM Account WHERE Id=:accountId LIMIT 1];

    account.Phone = newPhone;

    Boolean success = false; 

    try {
        update account; // Could throw a DMLException

        success = true; 
    } catch(DMLException e) { 
        // Adds error message to apex:PageMessages tag
        ApexPages.addMessage(e);
    }

    // Checks some condition, in this case, a boolean 
    if (success) {
        // Successful redirect 
        PageReference pageRef = new PageReference('/apex/myPage?AccountId=' + accountId);
        pageRef.setRedirect(true);

        return pageRef; 
    } else {
        // refresh page to show errors on failure 
        return null; 
    }
} 

This snippet may not be ideal, but should give you an idea where to start with handling error cases.

4
  • 1
    One suggestion, just use ApexPages.addMessages(e)
    – Adrian Larson
    Commented Sep 21, 2016 at 14:06
  • thank you for the explanations, but may I ask where the update of the account is executed ?
    – dou
    Commented Sep 21, 2016 at 14:14
  • @AdrianLarson, proper function call used. Commented Sep 21, 2016 at 14:22
  • @dou, I've updated the bottom snippet to call an update as opposed to throwing an exception, and added a comment to note that it could throw the exception for future readers. Hopefully this makes it clearer. Commented Sep 21, 2016 at 14:23

You must log in to answer this question.

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