title: Trying to clear ApexPages.Messages Container
body: <p>I apologize for the long winded question.</p>
  
  <p>Context:
  We have a mapping that basically maps a field from one object to a field on another object. Object_A to Object_B.  In the trigger of Object A we look to see if there is a mapping to object B.  If so, we load object b record and copy the value from the specific field in A to the field in B.  After we've made all the copies we update B.  </p>
  
  <p>We have a try catch around the update to B to catch any exceptions.  The issue we are facing is that an exception is thrown from a Salesforce validation, not a custom validation rule and creates messages automatically.  For example someone entered 123.45, and the field in object b that it's attempting to transfer to is formatted as a 2.0 number.  That will generate a DML exception for invalid range.  Which is fine.  The problem is that Salesforce is also adding an error to the apexPages.Messages container.  So if we have an  on the page it will display the Salesforce error.  </p>
  
  <p>There's 2 problems with this. 
  1. The message is not really user friendly. 
  2. The message contains the field label of the field on object B not the field on object A.  The user in this context will have no idea what that field is and with potentially 100 fields on the page that they have filled out, they might not be able to track it down. </p>
  
  <p>I would much rather SF didn't add the message automatically, allowing me to catch the exception and format a message.  But I don't see anyway to either stop SF from creating the message or clearing out the messages container so that we can add our own.  </p>
  
  <p>Is there a way to either stop Salesforce from automatically adding the message or to clear the apexPage.Messages without refreshing the page?</p>
  
  <p>edited to show a quick example code:</p>
  
  <pre><code>list&lt;Contact&gt; contacts = [Select id,Email from Contact where id='someSFId'];
  contacts[0].email = 'badEmail';
  try{
      update contacts;
  }
  catch(Exception ex)
  {
      System.debug(Apexpages.hasMessage());
  }
  </code></pre>
  
  <p>You'll see the results of the debug will be true even though I've not added a message. </p>