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.
DMLException
or not.Name
field. Does it save when you remove the name from thesoql
?FirstName
instead ofName
, orBillingStreet
and notBillingAddress
.