function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
NikiVankerkNikiVankerk 

Trying to fix invalid email in before insert trigger but hitting validation errors

From Salesforce documentation, the order of execution says that there is no system validation run prior to the 'before insert' trigger if the request is coming from sources other than standard UI, and that these will run instead after the 'before insert' trigger is executed.

Apex Developers Guide reference: http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_triggers_order_of_execution.htm?SearchType=Stem (http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_triggers_order_of_execution.htm?SearchType=Stem" target="_blank)

I'm trying to catch and fix bad email values from an external system sync in a before insert trigger, but when I test this in the Developer console, the before trigger is not even started before I get a System.DmlException error with INVALID_EMAIL_ADDRESS.  I'm running the test through the Execute Anonymous Window so that it is not submitted through the standard UI.  Has anyone else found how to get around the system validation in order to get into a before insert trigger?
Ajay_SFDCAjay_SFDC
Hi NikiVankerk ,

I faced the same issue. I used the following way to get around it :

1. Write one method which will validate the email in your handler class . If its valid then process with your next step and if its invalid then show error on page .
Because of this even if your validation rule fires such exception will not occur .
(Reason : We already validated email in our code. So while saving we will get valid email only)


public boolean validateEmail( String strEmail)
{
     String emailRegex =  '^[_A-Za-z0-9-+]+(\\.[_A-Za-z0-9-+]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$';
     Pattern MyPattern = Pattern.compile(emailRegex);
     Matcher MyMatcher = MyPattern.matcher(strEmail);
     if(!MyMatcher.matches()){
      return false;
     }
     else{
      return true;
     }
}

if(validateEmail('Your Email address'))
{
    // Means email is valid . Because of this validation rule will not cause any problem 
   // You can add your further operations here . 
}
else
{
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,
                          'The email entered is invalid, please re-enter.'));
}

Thanks ,
  Ajay

NikiVankerkNikiVankerk
Thanks for your thoughts.  Unfortunately I don't have access to the sync process that is pushing potentially invalid email addresses into the email type field, I can only try to catch them before the insert.  I ended up with the following workaround:
  1. created a new hidden email field of type text and updated sync process to push data into this field.
  2. before insert trigger looks at the data in this text field and if it matches a correct email pattern, i move the data into the original email type field that is displayed.

This way we don't encounter the invalid email errors because the sync is pushing into a text field but we get to see the valid email addresses at the end of the day.

I still don't know why the documentation is saying validation is skipped but I'm finding it is not being skipped, but here I managed to dodge the issue.