Solved

Help using JavaScript switch function w/MS Dynamics CRM 2011

Posted on 2011-03-03
3
1,416 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now