Ben Vollmer posted an article this morning about using the JavaScript Switch statement with a fairly good explanation of how it works and how it can be used as a replacement for the IF statement in certain situations.

That reminded me of a rather unusual Switch statement implementation that is found in the CRM SDK that I thought deserved a bit more exploration.

Before we get into that, let's take a quick look at the code Ben used in his article:

   1: var prefix = "";
   2: switch (crmForm.all.customertypecode.SelectedText)
   3: {
   4:      case "Manufacturer":
   5:                prefix="MFG";     
   6:                break;
   7:      case "Investor":
   8:                prefix="INV";
   9:                break;
  10:  
  11:      case "Press":
  12:                prefix="SCUM";
  13:                break;
  14:      default:
  15:                break;
  16: }
  17:  

 

This code checks the value of the customertypecode field and sets the value of a variable prefix accordingly.

If you've ever browsed around the CRM SDK, you may have seen the following code demonstrating the CRM Form OnChange event:

   1: // Attempt to auto-format basic US phone numbers.  This method supports
   2: // 7 and 10 digit numbers.  Example: (410) 555-1212
   3:  
   4: // Get the field that fired the event
   5: var oField = event.srcElement;
   6:  
   7: // If we have the field and all is well
   8: if (typeof(oField) != "undefined" && oField != null)
   9: {
  10:     // Remove any non-numeric characters
  11:     var sTmp = oField.DataValue.replace(/[^0-9]/g, "");
  12:  
  13:     // If the number is a length we expect and support, format the number
  14:     switch (sTmp.length)
  15:     {
  16:         case "4105551212".length:
  17:             oField.DataValue = "(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4);
  18:             break;
  19:  
  20:         case "5551212".length:
  21:             oField.DataValue = sTmp.substr(0, 3) + "-" + sTmp.substr(3, 4);
  22:             break;
  23:     }
  24: }

 

This too shows you how to utilize the Switch statement, but as you can see, it's very strange looking and a bit hard to understand at first. I had to look at it several times to figure out what they are doing and realized that it is a very cool piece of code indeed.

What this code does:

This code segment checks the length of a string then formats the string if it meets one of two criteria for length.  In reality, you could just have easily written the code as:

   1: // If the number is a length we expect and support, format the number
   2: switch (sTmp.length)
   3: {
   4:     case 10:
   5:         oField.DataValue = "(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4);
   6:         break;
   7:  
   8:     case 7:
   9:         oField.DataValue = sTmp.substr(0, 3) + "-" + sTmp.substr(3, 4);
  10:         break;
  11: }

 

Why this is cool code:

Instead of checking for a hard-coded length, the developer instead used a little JavaScript trick that allows you to work with properties of a literal value ( the stuff inside the quotes ).  This actually makes the code self-documenting.

At this point you're probably wondering what in the heck I am talking about. Well, hang on for a bit longer and I'll explain everything.

If the CRM developer had written the OnChange code as shown in my second example, we would be left to wonder, "Just what they heck do the values 10 and 7 have to do with anything?" Unless, of course, they inserted comments that described those values.

As it stands, what the case statement actually does( in pseudo code ):

If the length of the variable sTmp is equal to the value of a phone number in the format: "4105551212" then I would like format the Form field as follows….

If the length of the variable sTmp is equal to the value of a phone number in the format: "5551212" then I would like format the Form field as follows….

While this may seem a bit odd and confusing, it's actually very practical.  You can view the expected phone number variations while also seeing how the developer wished to handle the formatting for cases that matched their criteria.  To add additional formatting choices, all you must do is copy the case block of code and change the length test and the resultant formatting.

Clear as mud?