Solved

Help using JavaScript switch function w/MS Dynamics CRM 2011

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
rhino JavaScript import, load 25 66
unable to set breakpoints in chrome source tab 1 30
Accessing data in JSON response.d multiple objects 2 57
CSS Position not returning 2 47
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

911 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

22 Experts available now in Live!

Get 1:1 Help Now