Converting 10-Lines of Apex code to a 1-line Validation Rule Formula
Code clean-up is what I'm doing these days ... lots of code clean-up. One of our Salesforce.com orgs (we have sixteen of them) currently has 72% test coverage in production. I'm not sure how the previous administrators were able to install code below the 75% threshold, but they managed. I'm tasked with getting that code cleaned up, so I can deploy a new release.
While looking for areas to improve code coverage, I stumbled upon this trigger:
trigger checkAccountPhoneNumberBiBu on Account (before insert, before update) {
for (Account account : Trigger.new) {
if (account.Phone==null) continue;
Pattern p = Pattern.compile('[-() ]');
String sPhone = p.matcher(account.Phone).replaceAll('');
// check length without punctuation
if (sPhone.length() != 10) account.Phone.addError(' Phone number must have 3 digit area code and 7 digit number');
p = Pattern.compile('\\d');
sPhone = p.matcher(sPhone).replaceAll('');
if (sPhone.length() > 0) account.Phone.addError('Phone number must be formatted as (999)999-9999');
}
}
This trigger looks at the value entered in the "Phone" field before an Account record is inserted or updated; if the phone field is not in the (999)999-9999 format, it errors out and notifies the user to enter the phone # in the proper format.
In addition to this Apex code, the developer also had to write a testmethod to ensure coverage of the trigger. His code was only getting 67% test coverage (which is what brought the trigger to my attention in the first place).
As I started looking at what I needed to add to the testmethod to ensure 100% coverage, I realized it would be easier to just get rid of the trigger altogether, and replace it with a Validation Rule. That 10 lines of Apex code was reduced to a 1-line formula in a validation rule:
NOT(REGEX(Phone, "\\D*?(\\d\\D*?){10}"))
Code clean-up is what I'm doing these days ... lots of code clean-up. One of our Salesforce.com orgs (we have sixteen of them) currently has 72% test coverage in production. I'm not sure how the previous administrators were able to install code below the 75% threshold, but they managed. I'm tasked with getting that code cleaned up, so I can deploy a new release.
While looking for areas to improve code coverage, I stumbled upon this trigger:
trigger checkAccountPhoneNumberBiBu on Account (before insert, before update) {
for (Account account : Trigger.new) {
if (account.Phone==null) continue;
Pattern p = Pattern.compile('[-() ]');
String sPhone = p.matcher(account.Phone).replaceAll('');
// check length without punctuation
if (sPhone.length() != 10) account.Phone.addError(' Phone number must have 3 digit area code and 7 digit number');
p = Pattern.compile('\\d');
sPhone = p.matcher(sPhone).replaceAll('');
if (sPhone.length() > 0) account.Phone.addError('Phone number must be formatted as (999)999-9999');
}
}
This trigger looks at the value entered in the "Phone" field before an Account record is inserted or updated; if the phone field is not in the (999)999-9999 format, it errors out and notifies the user to enter the phone # in the proper format.
In addition to this Apex code, the developer also had to write a testmethod to ensure coverage of the trigger. His code was only getting 67% test coverage (which is what brought the trigger to my attention in the first place).
As I started looking at what I needed to add to the testmethod to ensure 100% coverage, I realized it would be easier to just get rid of the trigger altogether, and replace it with a Validation Rule. That 10 lines of Apex code was reduced to a 1-line formula in a validation rule:
NOT(REGEX(Phone, "\\D*?(\\d\\D*?){10}"))