NTBuddy

A place to learn about Microsoft SharePoint and Microsoft Dynamics CRM

Simple Phone Formatting

I'm always asked about a simple way to format the phone numbers in Dynamics CRM as they're a simple string. There are many options from simple JScript (shown below) that can work online or offline, advanced JScript (using a webservice) which requires the user to be in a connected state, or a pre or post callout which is more advanced and also doesn't provide instant feedback to the end user. The disadvantage of using JScript is it doesn't work with items being imported or through the web services interface where a pre/post callout can be used for this. The perfect solution is to combine the JScript and a pre/post callout to achieve the consistency on all of the ways of entering information within Dynamics CRM.

This article assumes you know how to first backup your information within CRM prior to making any customizations and that you understand how to make changes on a form to the onChange event of a form. In addition, I'm presume that you've made customizations in the past and that you're comfortable doing so. If you're not you're welcome to contact me with your questions and I'll be glad to assist you. The code I'm going to show you is a very basic JScript routine that I created that meets a majority of installation needs and is a great starter piece that can be easily copied and pasted into Dynamics CRM. The code will format as (xxx) xxx-xxxx Ext. xxxx and will default in the area code if the area code wasn't entered (e.g. the length is seven characters in length). You'll want to change the default area code to meet your organizations needs.

--- Begin Snippet ---

 

// Format phone numbers

function fncFormatPhone(sTemp) {

var sDefaultAreaCode = "574";

 

sTemp = sTemp.replace(/[^0-9]/g,""); //remove illegeal characters

sTemp = sTemp.substr(0, 14); // First 14 digits

 

if (sTemp.length == 7)              sTemp = "(" + sDefaultAreaCode + ") "   + sTemp.substr(0, 3) + "-" + sTemp.substr(3, 4);

else if (sTemp.length == 8)    sTemp = "(" + sTemp.substr(0, 3) + ") " + sTemp.substr(3, 3) + "-" + sTemp.substr(6, 2);

else if (sTemp.length == 9)    sTemp = "(" + sTemp.substr(0, 3) + ") " + sTemp.substr(3, 3) + "-" + sTemp.substr(6, 3);

else if (sTemp.length == 10)  sTemp = "(" + sTemp.substr(0, 3) + ") " + sTemp.substr(3, 3) + "-" + sTemp.substr(6, 4);

else if (sTemp.length == 11)  sTemp = "(" + sTemp.substr(0, 3) + ") " + sTemp.substr(3, 3) + "-" + sTemp.substr(6, 4) + " ext. 000"  + sTemp.substr(10,  1);

else if (sTemp.length == 12)  sTemp = "(" + sTemp.substr(0, 3) + ") " + sTemp.substr(3, 3) + "-" + sTemp.substr(6, 4) + " ext. 00"    + sTemp.substr(10,  2);

else if (sTemp.length == 13)  sTemp = "(" + sTemp.substr(0, 3) + ") " + sTemp.substr(3, 3) + "-" + sTemp.substr(6, 4) + " ext. 0"      + sTemp.substr(10,  3);

else if (sTemp.length == 14)  sTemp = "(" + sTemp.substr(0, 3) + ") " + sTemp.substr(3, 3) + "-" + sTemp.substr(6, 4) + " ext. "            +  sTemp.substr(10, 4);

 

return sTemp;

}

--- End Snippet ---

The above code will be used every where you want to add the phone code. Below is the step-by-step instructions for adding to the Account form for the main phone number. You will repeat these steps for each of the phone numbers in the system that you want to have this code on. Alternately, you can use scripts similar to the above to determine the format based on the country if the form has one, by the user, by the type of field  (phone vs. fax) for example.

  1. Log into your Microsoft Dynamics CRM system as a user with System Customizer rights
  2. Click on Settings
  3. Click on Customization
    1. Note: Don't forget to Export your customizations BEFORE and AFTER you've done all of these changes so you can revert back if there's a problem.
  4. Click on Customize Entities
  5. Double-click on Accounts
  6. Click on Forms and Views
  7. Double-click on Form
  8. Double-click on Main Phone
  9. Click on Schema and make note of the Schema Name as this is the name you will use to format in a few steps
  10. Click on Events
  11. Click on Edit as onChange is the only option you don't need to select this first
  12. Copy and paste the code from above
  13. Now for formatting the field you have two choices. The first choice is great as it's a simple copy/paste in the field and can be included in the snippet you copied above. The second is ideal if you're adding the formatting to the onLoad event of the form as you can specify the name of the field you want to format. I use the first one for the individual fields and the second for the onLoad event for this basic script.
    1. if (event.srcElement.DataValue != null) fncFormatPhone(event.srcElement.DataValue);
    2. if (crmForm.all.telephone1.DataValue != null) fncFormatPhone(crmForm.all.telephone1.DataValue);
  14. Now repeat this for every field you have on the form.

That's all there is to it. Now to test before saving and deploying your code.

Testing

Click on the Preview menu bar item and choose Create Form. You will now want enter text into all the fields you've entered and also REMOVE the text to make certain you have no errors within the form.

Saving

 If everything is good then click on Save and Close

Deploying

 Click Actions and choose Publish.

 
You're all set and ready to go for this form. Repeat the above steps above for each one of the forms that you want/need this code on. The forms can either be the core forms or the custom entities that you've created.

Additionally, items that code like the above can be used for our zip/postal code look up that fills in the city/state through a web service (or pre/post callout), formatting based on the country selected, validation of email and web addresses, etc. You're only limited by your imagination, budget and time that you have. With that being said remember that sometimes less is more so you need to properly way the balances for the customizations you make to your system.

Posted: Oct 31 2007, 05:10 AM by NTBuddy | with no comments
Filed under: