locked
java script and Auto format Telephone and Fax number in CRM 2011 RRS feed

  • Question

  • Hi Guys,

    I am currently trying to auto format my Main telephone + and Fax number in the CRM 2011. My problem here is that I am unable to automate the "Fax" number. But the Main telephone number is able to be autoformated based on the code i have provided below.

    I am pretty new to java scripting, so i don't know where i'm making the mistake. The codes for both functions are essentially the same with the exception of variable names  being changed to their respectable field.

    I would greatly appreciate if anyone can solve my problem or identify the issue with my code.

    Thanks in advance!

    function dsl_FormatPhone()// Function for main Telephone
    {
    var InputPhone = Xrm.Page.getAttribute( "telephone1" ).getValue();
      if( InputPhone != null)
       {
        var regexObj = /^(?:\+?1[-. ]?)?(?:\(?([0-9]{3})\)?[-. ]?)?([0-9]{3})[-. ]?([0-9]{4})$/;
        if (regexObj.test(InputPhone))
        {
        var parts = InputPhone.match(regexObj);
        var phone = "";
         if (parts[1]) 
            {
            phone += " (" + parts[1] + ") "; 
         //       phone +=  + parts[1] + "-"; 
             }
        phone += parts[2] + "-" + parts[3];
        //alert("Phone # is " + phone);
        Xrm.Page.getAttribute("telephone1").setValue(phone);
       }
      }
    }
    
    function dsl_FormatPhone2() //function for fax numbers
    {
    var InputPhone2 = Xrm.Page.getAttribute( "fax" ).getValue();
      if( InputPhone2 != null)
       {
        var regexObj = /^(?:\+?1[-. ]?)?(?:\(?([0-9]{3})\)?[-. ]?)?([0-9]{3})[-. ]?([0-9]{4})$/;
        if (regexObj.test(InputPhone2))
        {
        var parts = InputPhone2.match(regexObj);
        var phone = "";
         if (parts[1]) 
            {
            phone += " (" + parts[1] + ") "; 
         //       phone +=  + parts[1] + "-"; 
             }
        phone += parts[2] + "-" + parts[3];
        //alert("Phone # is " + phone);
        Xrm.Page.getAttribute("fax").setValue(phone);
       }
      }
    }



    • Edited by bobsays48 Monday, March 3, 2014 10:54 PM
    Monday, March 3, 2014 10:52 PM

All replies

  • Hi,

          The code seems fine. Have you enabled the code in CRM form for the fax field?


    Hope this helps.
     
    -----------------------------------------------------------------------
     Minal Dahiya
    blog : http://minaldahiya.blogspot.com.au/

     
    If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful"

    Tuesday, March 4, 2014 12:04 AM
  • Hi Minal,

    I'm still very new with the CRM, but if you meant creating a new web resource for the java-script and adding it to event handler, then yes i have;

    Tuesday, March 4, 2014 12:13 AM
  • Hi,

        Can we do a quick test? If you put just a simple alert ("I am here"); instead of the whole code do you get the message?

    function dsl_FormatPhone2() //function for fax numbers
    {
    alert("I am here");
    }

    If you do, I would suggest uncomment the alert in your code. Do you get the alert? What is the message displayed?

    Please let me know how you go.

    -Minal

    Tuesday, March 4, 2014 12:28 AM
  • Hi,

    I think you had problem with putting your function dsl_FormatPhone2() on event.

    OR

    You can debug your js code by putting "debugger;" text starting of your code and check if you are able to debug the code.


    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Proposed as answer by DynamicsCRM31 Tuesday, March 4, 2014 10:49 AM
    Tuesday, March 4, 2014 10:49 AM
  • Hi Minal,

    I got rid of the code and replaced with the simple alert code. However, it does not seem like anything is happening when i preview the form after saving it.


    Tuesday, March 4, 2014 4:11 PM
  • 


    Not sure if this is any helpful. But I'm assuming this is what you were talking about ? 

    Tuesday, March 4, 2014 4:22 PM
  • The thing that i don't understand is the fact that the code is exactly the same but it works for regular number but not fax. It is very frustrating
    Tuesday, March 4, 2014 4:29 PM
  • Hi bobsay,

    Did you try entering the exact same values in both telephone and fax fields and check if it's working for telephone and not for fax?


    Admin QuikView Solution for CRM 2013

    Tuesday, March 4, 2014 5:08 PM