Solved

Help using JavaScript switch function w/MS Dynamics CRM 2011

Posted on 2011-03-03
3
1,418 Views
Last Modified: 2012-06-27
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
Comment
Question by:savetheorcas
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
ID: 35033399
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
 

Author Closing Comment

by:savetheorcas
ID: 35033598
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
 
LVL 16

Expert Comment

by:sjklein42
ID: 35033614
So glad.  That's why I avoid fancy things like SWITCH statements.  KISS, they say!  :)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question