Quantcast
Channel: microsoft dynamics crm partner canada – CRM Software Blog | Dynamics 365
Viewing all articles
Browse latest Browse all 30

How To: Standardize Telephone Numbers with Javascript in Microsoft Dynamics CRM

$
0
0

How to add Telephone Number Standardizing Javascript to CRM 2011 and 2013:

Instructions for CRM 2011:

The script found below will take a string of 10 numbers and convert them to the following format: (###) ###-####.

  • Identify the script to be used:

function FormatPhoneNumber(fieldName)

{

    var oField = Xrm.Page.getAttribute(fieldName);

    var sAllNumeric = oField;

    if (typeof (oField) != "undefined" && oField.getValue() != null)

    {

        sAllNumeric = oField.getValue().replace(/[^0-9]/g, "");

        switch (sAllNumeric.length)

        {

            case 10:

                sFormattedPhoneNumber = "(" + sAllNumeric.substr(0, 3) + ") " + sAllNumeric.substr(3, 3) + "-" + sAllNumeric.substr(6, 4);

                oField.setValue(sFormattedPhoneNumber);

                break;

            default:

                alert("Phone must contain 10 numbers.")

                break;

        }

    }

}

  • Open up the solution that you wish to make the customization in
  • Click on Web resources in the left hand navigation pane
  • Click on New to add a new Web Resource
    • Name: PhoneFormatting
    • Display Name: Phone Formatting
    • Description: Auto format jscript for any phone fields
    • Type: Script (JScript)
    • Click on Text Editor

i.    Copy the javascript in step 1 and past into the big text box, then click on OK

    • Language: English
    • Click on Save and Close
  • Open the Form of the Entity that you would like the Javascript applied to, ie: Contact Form named Information
  • Click on Form Properties
  • Under Form Libraries click on Add and select the Web Resource you created in step #4. Select OK and close this window

Note: Do NOT add any Event Handlers in this screen.

  • Click on the field you would like to auto-format using this script
  • Click on Change Properties
  • Click on the Details tab and take note of the text in the field Name – this will be used in step 12b
  • Click on the Events tab
  • Under Event Handlers
    • Make sure Event is OnChange
    • Click on Add

i.      Library: Select your appropriate Web Resource created in step #2 and applied to the form in step #5

ii.      Function: When using the script found in step one, enter in the following text – FormatPhoneNumber

iii.      Enabled: Make sure this is checked

iv.      Pass execution context as first parameter: Make sure this is not checked

v.      Enter the name of the field found in step 8 in the text box with quotation marks around it – “telephone1”

vi.      Click on OK

    • Click on OK
  • Complete steps 8-12 for any remaining telephone / fax fields that you would like to auto-format within this form
  • Save and Close this form
  • Repeat steps 5-12 for any other entities that you would like to apply this formatting to
  • Publish All Customizations
  • Test your customization by entering numbers in the fields you used on the entities you modified

 

Instructions for CRM 2013:

The script found below will take a string of 10 numbers and convert them to the following format: (###) ###-####.

  • Identify the script to be used:

function FormatPhoneNumber(fieldName)

{

    var oField = Xrm.Page.getAttribute(fieldName);

    var sAllNumeric = oField;

    if (typeof (oField) != "undefined" && oField.getValue() != null)

    {

        sAllNumeric = oField.getValue().replace(/[^0-9]/g, "");

        switch (sAllNumeric.length)

        {

            case 10:

                sFormattedPhoneNumber = "(" + sAllNumeric.substr(0, 3) + ") " + sAllNumeric.substr(3, 3) + "-" + sAllNumeric.substr(6, 4);

                oField.setValue(sFormattedPhoneNumber);

                break;

            default:

                alert("Phone must contain 10 numbers.")

                break;

        }

    }

}

  • Open up the solution that you wish to make the customization in
  • Click on Web resources in the left hand navigation pane
  • Click on New to add a new Web Resource
    • Name: PhoneFormatting
    • Display Name: Phone Formatting
    • Description: Auto format jscript for any phone fields
    • Type: Script (JScript)
    • Click on Text Editor

i.      Copy the javascript in step 1 and past into the big text box, then click on OK

    • Language: English
    • Click on Save
  • Open the Form of the Entity that you would like the Javascript applied to, ie: Contact Form named Contact
  • Click on Form Properties
  • Under Form Libraries click on Add and select the Web Resource you created in step #2 and click on Add. Click on OK to close the Form Properties window

Note: Do NOT add any Event Handlers in this screen.

  • Click on the field you would like to auto-format using this script.
  • Click on Change Properties
  • Click on the Details tab and take note of the text in the field Name – this will be used in step 12b
  • Click on the Events tab
  • Under Event Handlers
    • Make sure Event is OnChange
    • Click on Add

i.      Library: Select your appropriate Web Resource created in step #2 and applied to the form in step #7

ii.      Function: When using the script found in step one, enter in the following text – FormatPhoneNumber

iii.      Enabled: Make sure this is checked

iv.      Pass execution context as first parameter: Make sure this is not checked

v.      Enter the name of the field found in step 8 in the text box with quotation marks around it – “telephone1”

vi.      Click on OK

    • Click on OK to close the Field Properties window
  • Complete steps 8-12 for any remaining telephone / fax fields that you would like to auto-format.
  • Click on Save and Close for this form
  • Repeat steps 5-12 for any other entities that you would like to apply this formatting to
  • Click on Publish All Customizations
  • Test your customization by entering numbers in the fields you used on the entities you modified.

 

BDO Solutions is a national firm with local practices throughout Canada, including Ontario, Alberta, British Columbia, Manitoba and Saskatchewan. Microsoft Gold Certified Partner and Reseller of the Year in Canada for 2010, 2011 and 2012.

By Tara Sinkinson, Microsoft Dynamics CRM Solutions Consultant with BDO Solutions, Canada.

The post How To: Standardize Telephone Numbers with Javascript in Microsoft Dynamics CRM appeared first on CRM Software Blog | Dynamics 365.


Viewing all articles
Browse latest Browse all 30

Latest Images

Trending Articles





Latest Images