{"id":86,"date":"2013-10-05T15:17:30","date_gmt":"2013-10-05T23:17:30","guid":{"rendered":"http:\/\/www.bridgefarmconsulting.com\/blog\/?p=86"},"modified":"2013-10-08T14:50:43","modified_gmt":"2013-10-08T22:50:43","slug":"email-validation-in-apex","status":"publish","type":"post","link":"https:\/\/www.bridgefarmconsulting.com\/blog\/email-validation-in-apex\/","title":{"rendered":"Email Validation in APEX"},"content":{"rendered":"<p>If you enter an email address into an &#8217;email&#8217; type field in Force.com, it will validate that the value entered is a valid email, and won&#8217;t let you save the record with a bad email. But when you are integrating Salesforce with another system, that system may not perform the same level of verification. You often need to import the record into Salesforce regardless of whether the email is valid, but need to be able to handle bad emails so you don&#8217;t try and save a record with a bad email address.<\/p>\n<p>So I always create a static method in a Utility class that I can use to check an email and return a boolean to tell me if the email is valid or not. If not, I can then save the email address to a non-email text field, and flag the record for review by a business user. The method uses a pattern match with a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Regular_expression\" target=\"_blank\">REGEX<\/a> for matching against emails. I have always used the example from <a target=\"_blank\" href=\"http:\/\/salesforcesource.blogspot.com\/2010\/01\/utilizing-apex-pattern-and-matcher.html\">this post<\/a>. But if you search for Email and Regex, you&#8217;ll find lots of examples. The one below seems to handle most scenarios though.<\/p>\n<p><code><br \/>\npublic static Boolean checkEmail (String semail) {<br \/>\nString InputString = semail;<br \/>\nString emailRegex = '([a-zA-Z0-9_\\\\-\\\\.]+)@((\\\\[a-z]{1,3}\\\\.[a-z]{1,3}\\\\.[a-z]{1,3}\\\\.)|(([a-zA-Z0-9\\\\-]+\\\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})';<br \/>\nPattern MyPattern = Pattern.compile(emailRegex);<\/p>\n<p>\/\/ Then instantiate a new Matcher object \"MyMatcher\"<br \/>\nMatcher MyMatcher = MyPattern.matcher(InputString);<\/p>\n<p>if (!MyMatcher.matches()) {<br \/>\nreturn FALSE;<br \/>\n}<br \/>\nelse {<br \/>\nreturn TRUE;<br \/>\n}<br \/>\n} \/\/end email check<br \/>\n<\/code><br \/>\nAnd then in my integration code, a simple check<br \/>\n<code><br \/>\nif (UtilitiesDemo.checkEmail (sEmail) ) {<br \/>\n\/\/Use email<br \/>\n}<br \/>\nelse {<br \/>\nhandle bad email<br \/>\n}<br \/>\n<\/code><br \/>\nYou can see a working example <a href=\"http:\/\/bfcblog-developer-edition.na9.force.com\/EmailValidationExample\" target=\"_blank\">here<\/a><\/p>\n<p>Next up, formatting phone numbers from another system&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you enter an email address into an &#8217;email&#8217; type field in Force.com, it will validate that the value entered is a valid email, and won&#8217;t let you save the record with a bad email. But when you are integrating Salesforce with another system, that system may not perform the same level of verification. You [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,8],"tags":[],"class_list":["post-86","post","type-post","status-publish","format-standard","hentry","category-apex","category-utilities"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.bridgefarmconsulting.com\/blog\/wp-json\/wp\/v2\/posts\/86","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bridgefarmconsulting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bridgefarmconsulting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bridgefarmconsulting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bridgefarmconsulting.com\/blog\/wp-json\/wp\/v2\/comments?post=86"}],"version-history":[{"count":9,"href":"https:\/\/www.bridgefarmconsulting.com\/blog\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"predecessor-version":[{"id":117,"href":"https:\/\/www.bridgefarmconsulting.com\/blog\/wp-json\/wp\/v2\/posts\/86\/revisions\/117"}],"wp:attachment":[{"href":"https:\/\/www.bridgefarmconsulting.com\/blog\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bridgefarmconsulting.com\/blog\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bridgefarmconsulting.com\/blog\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}