Solved

Help using JavaScript switch function w/MS Dynamics CRM 2011

Posted on 2011-03-03
3
1,437 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to create an extensible mechanism for linked drop downs.
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…

717 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