locked
Masking Phone Numbers in CRM 2011 RRS feed

  • Question

  • We are implementing MS CRM 2011. We would like to mask U.S. phone numbers to the standard (999) 999-9999 format using the JS Web Resource.

    I searched for and used the following code from: http://www.dynamicscrmtrickbag.com/2010/12/24/re-usable-jscript-libraries-in-dynamics-crm-2011/

    function FormatPhoneNumber(context)
    {
     var oField = context.getEventSource().getValue();
     var sTmp = oField;

     if(typeof(oField)!= "undefined" && oField!= null)
     {
       sTmp = oField.replace(/[^0-9]/g, "");
       switch (sTmp.length)
       {
         case 10:
          sTmp = "(" + sTmp.substr(0,2) + ")" + sTmp.substr(3,3) + sTmp.substr(6,4);
          break;

        default:
         alert("Phone must contain 10 numbers.")
        break;
      }
     }
     context.getEventSource().setValu(sTmp);
    }

    However, when I tried to implement it in the account view according to the instructions, it caused the CRM top ribbon to disappear. And the code did not work.

    I also tried the code at: http://social.microsoft.com/Forums/en-US/crm/thread/30f35e31-9a28-430f-91cb-7d8c8102383b with the same result.

    Any help would be appreciated. Thanks!

    Wednesday, March 30, 2011 7:58 PM

All replies

  • I hate to be the one to point it out, but looks like you forgot your "e" in setValue  -  should be context.getEventSource().setValue(sTmp);

     

    this works.

    function

     

    FormatPhoneNumber(context)

    {

    var oField = context.getEventSource().getValue();

    var

    sTmp = oField;

     

    if (typeof (oField) != "undefined" && oField != null)
    {
    sTmp = oField.replace(/[^0-9]/g,
    ""

    );

     

    switch

    (sTmp.length)
    {

     

    case

    10:

    sTmp =

    "(" + sTmp.substr(0, 3) + ")" + sTmp.substr(3, 3) + "-"

    + sTmp.substr(6, 4);

     

    break

    ;

     

    default

    :

    alert(

    "Phone must contain 10 numbers."

    )

     

    break

    ;

    }

    }

    context.getEventSource().setValue(sTmp);

    }

     

     

    Thursday, April 14, 2011 12:34 AM
  • Hi and thanks for the tip and code!

    I implemented it into CRM 2011 using the steps at http://www.dynamicscrmtrickbag.com/2010/12/24/re-usable-jscript-libraries-in-dynamics-crm-2011/.

    The code works its formatting magic however, whenever an account is loaded, an error message pops up:

    "There was an error with this field's customized event.

    Field:window

    Event:onload

    Error:Object doesn't support this property or method"

    Any ideas as to what is wrong?

    Thanks in advance!

    • Proposed as answer by vasubabuk Tuesday, March 19, 2013 6:31 AM
    Tuesday, April 19, 2011 5:48 PM