locked
CRM 2011 - adding a '1' in front of mobile number RRS feed

  • Question

  • We have a field for collecting a mobile number.  To be able to send SMS to the contact, we need the number to be 1+10digit number.  What is the simplest method to insert a '1' at the beginning of the number, either at the input or via workflow after the fact?  I am open for any method, other than relying on the end-user to enter the '1' :)  Thanks in advance!

    Wednesday, February 4, 2015 2:22 PM

All replies

  • There are a few options.  Here's a couple.

    1) Assuming input is through a CRM form you can add a JavaScript on change event to the mobile number form which checks the number format and adds the 1 if required.

    2) You can write a .NET plugin (or custom workflow step) to do the same.

    Here's some sample JavaScript which doesn't do exactly what you want, but could easily be modified to your purposes:

    function PhoneNumberFormat(fieldName)
    {
    var oField = Xrm.Page.getAttribute(fieldName);
    
    if (oField.getValue() != "undefined" && oField.getValue() != null)
    {
    
    oField.setValue(oField.getValue().replace("+1-",""));
    oField.setValue(oField.getValue().replace("+1 ",""));
    oField.setValue(oField.getValue().replace("+1",""));
    oField.setValue(oField.getValue().replace("+",""));
    
    // Remove any nonnumeric characters.
     var sTmp = oField.getValue().replace(/[^0-9]/g, "");
    
    // If the number has a valid length, format the number.
     switch (sTmp.length)
     {
    
       case 11:
    
         oField.setValue(sTmp.substr(0, 1) + "-" + sTmp.substr(1, 3)  +"-"+ sTmp.substr(4, 3)+ "-" + sTmp.substr(7, 4));
    
         break;
       case 10:
    
         oField.setValue(sTmp.substr(0, 3) + "-" + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4));
    
         break;
    
       case 7:
    
         oField.setValue(sTmp.substr(0, 3) + "-" + sTmp.substr(3, 4));
    
         break;
    
     }
     if (sTmp.substr(0,1) != "1" )oField.setValue("+1-"+oField.getValue());
     else oField.setValue("+"+oField.getValue())
    
    }


    Wednesday, February 4, 2015 5:24 PM
  • Try below code in the form load:

    function PhoneValidationwithOnePlusExtension()
     {
    
    var oPhone= Xrm.Page.getAttribute("mobilephone").getValue();
    if(oPhone!=null && oPhone!="")
    {
    var plus=oPhone.substring(0, 1);
    if(plus!="+")
    {
    var replaceValue = "+1" +oPhone;
    Xrm.Page.getAttribute("mobilephone").setValue(replaceValue);
    }
    }
    
     }


    • Proposed as answer by Mohd Saad Thursday, February 5, 2015 6:22 AM
    Thursday, February 5, 2015 6:20 AM