0

Javascript we had for Unified interface of Dynamics 365 to format phone numbers was working perfectly until the latest update, now it only works in custom interface and has stopped working in UI, anybody has any idea how this can be fixed?

var XXX = window.XXX || {};
(function() {

    // Code to run in the form OnLoad event
    this.formOnLoad = function(executionContext) {
      var formContext = executionContext.getFormContext();

      // display the form level notification as an INFO
      formContext.ui.setFormNotification(message, "INFO", myUniqueId);

      // Wait for 5 seconds before clearing the notification
      window.setTimeout(function() {
        formContext.ui.clearFormNotification(myUniqueId);
      }, 5000);
    }

    // Code to run in the attribute OnChange event
    this.mobilePhoneFormatting = function(executionContext) {
      var formContext = executionContext.getFormContext();
      var mobilePhone = formContext.getAttribute("mobilephone").getValue();
      var formatPhone = "";
      try {
        if (mobilePhone != null) {
          var phoneNumbers = mobilePhone.replace(/\D/g, '');
          if (phoneNumbers.length == 10) { //10 digit case. Output adds +1 and proper format
            formatPhone = ("+1 (" + phoneNumbers.substring(0, 3) + ") " + phoneNumbers.substring(3, 6) + "-" + phoneNumbers.substring(6, 10));
          } else if (phoneNumbers.length == 11) { //11 digit case. Output proper format
            formatPhone = ("+" + phoneNumbers.substring(0, 1) + " (" + phoneNumbers.substring(1, 4) + ") " + phoneNumbers.substring(4, 7) + "-" + phoneNumbers.substring(7, 11));
          } else if (phoneNumbers.length == 14) { //14 digit case. Without Country code and with extension
            formatPhone = ("+1 (" + phoneNumbers.substring(0, 3) + ") " + phoneNumbers.substring(3, 6) + "-" + phoneNumbers.substring(6, 10) + " x" + phoneNumbers.substring(10, 14));
          } else if (phoneNumbers.length == 15) { //15 digit case. With Country code and extension
            formatPhone = ("+" + phoneNumbers.substring(0, 1) + " (" + phoneNumbers.substring(1, 4) + ") " + phoneNumbers.substring(4, 7) + "-" + phoneNumbers.substring(7, 11) + " x" + phoneNumbers.substring(11, 15));
          } else if (phoneNumbers.length == 4) { //4 digit case. Extension Only
            formatPhone = ("x" + phoneNumbers.substring(0, 4));
          } else {
            formatPhone = mobilePhone;
          }
          formContext.getAttribute("mobilephone").setValue(formatPhone);
          formContext.data.entity.save();
        }
      } catch (err) {
        txt = "There was an error formatting the Phone Number.\n\n";
        txt += "Error description: " + err.message + "\n\n";
        txt += "Click OK to continue.\n\n";
        alert(txt);
      }
    }
  • any console errors? – Naga Sai A Jul 30 '19 at 14:15
  • I get this error: TypeError: executionContext.getFormContext is not a function – Chandini Ramesh Jul 30 '19 at 16:27
  • Microsoft explicitly state not to use the DOM with custom JS. If you do use DOM functionality it will likely break on upgrades - as has happened here. I would suggest the first port of call is to re-write the functionality not using the DOM. – RedCrusador Jul 31 '19 at 15:20
  • Is it called from the ribbon? Then perhaps this: `var formContext = primaryControl;`? [ref](https://stackoverflow.com/a/54012344/4003419) – LukStorms Nov 18 '19 at 18:45

0 Answers0