Formatting Phone Numbers in CRM 2011

Tuesday, July 19, 2011

The CRM 4.0 SDK had a great example of formatting a phone number for the US.

For CRM 2011, that sample isn’t in the SDK that I could find.So after looking at the SDK sample, it looked like it would be pretty easy to upgrade it to CRM 2011.

Here is the script modified

function FormatPhone(context)
{
// Get the field that fired the event.
var oField = context.getEventSource().getValue();
var sTmp = oField 

// Validate the field information.
if (typeof(oField) != "undefined" && oField != null)
{
 // Remove any non-numeric characters.
 var sTmp = oField.replace(/[^0-9]/g, "");

 // If the number has a valid length, format the number.
 switch (sTmp.length)
 {
  case "4105551212".length:
  oField = "(" + 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;
 }
}
}

 

When you put the web resource on the field, check the Pass execution context as first parameter.

image

You now have phone number formatting on each field you have this script on!

Filed Under: Demo Code

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading