Phone Number Formatting in CRM

Roshan Mehta, 06 May 2010

I have seen first-hand many phone number details for a customer record stored in a messy format. Sometimes the phone number is missing a country code or an area code, or simply has been entered so quickly into the system, that the user entering the data completely missed out a digit. This can cause frustration when another user opens up a customer record to find that the phone number hasn't been entered correctly, making it impossible to call the customer. The problem becomes worse when you realise that no other contact information has been entered for the customer, so you will not be able to follow up with them at all.

I have been working on a solution to this problem for one of our clients who requested that all phone numbers be stored in an international format (e.g. +64 9 123 4567), much like the IPhone does it. The idea here is that when a new Contact is created, a user must enter a phone number in one of the following New Zealand formats:

Land Line Numbers
+64 9 123 4567
00 64 9 123 4567

Mobile Phone Numbers
+64 21 123 456
00 64 21 123 456
+64 21 123 4567
00 64 21 123 4567
+64 21 1234 5678
00 64 21 1234 5678

Business Numbers
0508 123 456
0800 123 456
0900 123 456

The reason that a user needs to enter the phone number in one of the above formats is to eliminate the need to "guess" the country code and area code details. If a user enters a New Zealand phone number in any of the formats shown above, the plug-in will automatically display the number in the correct format by applying the correct number of spaces between each set of digits.

There are currently three formats for business numbers in New Zealand - 0508, 0800, and 0900. If a user enters a number in any business format e.g. "0800 123 456", the plug-in will format this number correctly, for example "+64 800 123 456".

How it currently works:
The plug-in reads a number entered into any phone number field on the Contact form - this includes land line numbers, mobile numbers, and business numbers as shown above. It then cleans the data by removing any white space characters from the phone number so it is ready for a validity check. It then checks the phone number for a match against a set of regular expression patterns.

The plug-in is registered for the create and update events of the Contact entity. This ensures that phone number formatting will still be applied, should a CRM user modify a Contact's phone number.

Inner Workings:
For the plug-in to format phone numbers for any geographical location, we have created a few custom entities behind the scenes within the settings area which store special regular expression patterns for that particular location. Let’s say we want to format New Zealand mobile numbers which are always in the form: +64 2# ### ### (assuming we have six digits after the 02#). All we need to do is create a new record for “New Zealand” which will define the regular expression pattern. The plug-in will then test an inputted number against this pattern and return the mobile number in the correct format.