You need to sign in to do that
Don't have an account?
NikiVankerk
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?
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?
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
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.