[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1465
  • Last Modified:

Help using JavaScript switch function w/MS Dynamics CRM 2011

I am attempting to use a switch statement within MS Dynamics CRM 2011 using form scripting. What I am trying to achieve is this:

When a user enters a U.S. state abbreviation in either the account or contact entity, I would like the abbreviation to be replaced by the full state name. For example, an entry of "WA" should automatically become "Washington" triggered by an onchange event ... and so forth.

I have tried many different versions of the following code, but I have no success:

In case you are not familiar with MS Dynamics, please refer to the attached code with the function formatPhone(), which does work.
function formatPhone()
{
		
// Regular Phone Numbers
var oField = event.srcElement;
		
		
// Remove any nonnumeric characters.
var sTmp = oField.DataValue.replace(/[^0-9]/g, "");
		
if (oField.DataValue != "undefined" && oField.DataValue != null) {
	// If the number has a valid length, format the number.
	switch (sTmp.length) {
		
		case "4105551212".length:
		    oField.DataValue = "+1 (" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4);
		    break;
		
		case "14105551212".length:
		    oField.DataValue = "+" + sTmp.substr(0, 1) + " (" + sTmp.substr(1, 3) + ") " + sTmp.substr(4, 3) + "-" + sTmp.substr(7, 4);
		    break;
		
		case "141055512121".length:
		    oField.DataValue = "+" + sTmp.substr(0, 1) + " (" + sTmp.substr(1, 3) + ") " + sTmp.substr(4, 3) + "-" + sTmp.substr(7, 4) + " Ext. " + sTmp.substr(11, 1);
		    break;
		
		case "1410555121212".length:
		    oField.DataValue = "+" + sTmp.substr(0, 1) + " (" + sTmp.substr(1, 3) + ") " + sTmp.substr(4, 3) + "-" + sTmp.substr(7, 4) + " Ext. " + sTmp.substr(11, 2);
		    break;
		
		case "14105551212123".length:
		    oField.DataValue = "+" + sTmp.substr(0, 1) + " (" + sTmp.substr(1, 3) + ") " + sTmp.substr(4, 3) + "-" + sTmp.substr(7, 4) + " Ext. " + sTmp.substr(11, 3);
		    break;
		
		case "141055512121234".length:
		    oField.DataValue = "+" + sTmp.substr(0, 1) + " (" + sTmp.substr(1, 3) + ") " + sTmp.substr(4, 3) + "-" + sTmp.substr(7, 4) + " Ext. " + sTmp.substr(11, 4);
		    break;
		
		case "1410555121212345".length:
		    oField.DataValue = "+" + sTmp.substr(0, 1) + " (" + sTmp.substr(1, 3) + ") " + sTmp.substr(4, 3) + "-" + sTmp.substr(7, 4) + " Ext. " + sTmp.substr(11, 5);
		    break;
		
		case "14105551212123456".length:
		    oField.DataValue = "+" + sTmp.substr(0, 1) + " (" + sTmp.substr(1, 3) + ") " + sTmp.substr(4, 3) + "-" + sTmp.substr(7, 4) + " Ext. " + sTmp.substr(11, 6);
		    break;
	}
}
}

Open in new window

function formatState() {

// Get the field that fired the event
var oField = event.srcElement;
var state = oField.DataValue;

switch (state) {
	case "WA":
	    oField.DataValue = "Washington";
	    break;
}

}

Open in new window

0
savetheorcas
Asked:
savetheorcas
  • 2
1 Solution
 
sjklein42Commented:
Hi.

You may already have tried this, but here's the simple alternative to the SWITCH statement.

None of my research on the Javascript SWITCH statement shows a single example of it working for strings - just numbers.  Maybe it doesn't work for strings?  Your Phone number example is switching on a numeric variable.

Try this:

function formatState() {

// Get the field that fired the event
var oField = event.srcElement;
var state = oField.DataValue;

if      (state == "WA") { oField.DataValue = "Washington"; } 
else if (state == "PA") { oField.DataValue = "Pennsylvania"; }
else if (state == "NH") { oField.DataValue = "New Hampshire"; }
...

}

Open in new window

0
 
savetheorcasAuthor Commented:
Wow, it worked!! Thank you so very much, I've been going crazy with this trying to get it to work. I wonder if you are right about switch not working for strings.

Thanks again!
0
 
sjklein42Commented:
So glad.  That's why I avoid fancy things like SWITCH statements.  KISS, they say!  :)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now