Dec 12 2007

Auto-formatting Phone Numbers in Microsoft CRM

While teaching a custom Microsoft CRM 3.0 class for a company, I had a number of students who were asking me about the phone number format in the Account and Contact forms. The issue is that Microsoft CRM accepts multiple formats including dashes, parenthesis and other format combinations and variations. The ability to auto-format various phone number formats that users entered into a common format had been present in their previous CRM software. However the out of the box, Microsoft CRM 3.0 does not auto-format phone number fields in the Lead, Account and Contact forms.

This can lead to a possible annoying and unmanageable headaches. I provided the client with a client side code solution from the Microsoft SDK for Microsoft CRM along with an additional line of code for better functionality which avoids a problem with a change to valid data to the phone number field to a null value which would have produced a Jscript error in the OnChange event without the additional line to the code.

The highlighted line items below are the additional items added to the Microsoft SDK code. The code below should change any 7 or 10 digit phone number format to the following format: 555-555-5555.
// Get the field that fired the event.
var oField = event.srcElement;

// Validate the field information.
if (typeof(oField) != “undefined” && oField != null)

if (oField.DataValue != null)
{

// Remove any nonnumeric characters.
var sTmp = oField.DataValue.replace(/[^0-9]/g, “”);

// If the number has a valid length, format the number.
switch (sTmp.length)
{
case “4105551212″.length:
oField.DataValue = “(” + sTmp.substr(0, 3) + “) ” + sTmp.substr(3, 3) + “-” + sTmp.substr(6, 4);
break;

case “5551212″.length:
oField.DataValue = sTmp.substr(0, 3) + “-” + sTmp.substr(3, 4);
break;
}
}

}

Remarks:
You can force the OnChange event to occur for a check box control from the Microsoft CRM form OnLoad event or from the OnChange event of some other field. To do this, use the FireOnChange method.

For example, you may use the following sample script:

crmForm.all.YourFieldID.FireOnChange();

Trackback URI | Comments RSS

Leave a Reply