Error Handling in MS-CRM
1) Handling UnExpected User Input
Some standard field formats in CRM include elements of user input
validation. Upon data entry, users are prompted if their input is
invalid, and the focus returns to the field, while the incorrect
input is cleared. Take the date field as an example. Enter in a
date field a string of abc123 to observe the behavior. The
following prompt gets displayed:
Once you click on OK, your input is cleared, and the focus is
returned to the field you were trying to modify. But let's see how
we can achieve the same result on a standard text field.
We want to check that the input starts with a capital letter, is
longer than three characters, and does not contain spaces. If these
requirements are not met, we'll bring up a notification
message,
clear the field, and return the focus to it.
HOW To Achieve Desired Result
function CheckUserInput()
{
var _userInput = Xrm.Page.getAttribute("new_singlename").
getValue();
var _isValid = false;
if(_userInput != null && _userInput != "")
{
if(_userInput.match(/^[A-Z][a-z]+$/))
{
www.it-ebooks.info
Chapter 5
111
_isValid = true;
}
}
if(_isValid == false)
{
// clear the field
_userInput = "";
Xrm.Page.getAttribute("new_singlename").
setValue(_userInput);
// alert
alert("The input is not valid!");
// set focus
Xrm.Page.getControl("firstname").setFocus(true);
Xrm.Page.getControl("new_singlename").setFocus(true);
}
}
In the first part, we retrieve the user input, we check to make
sure that there is in fact some input, and we run it against a regular
expression.
In the second part, once we have determined that there is no match,
we execute our script to clear the field, bring up the notification
alert for the user, and set the focus back to the same field.
2) Types Of Error encountered in General with MS-CRM
a) Generating a URIError: We are decoding a string that is not a valid
URL, resulting in the following type of error:
try
{
decodeURIComponent("%");
}
b) Generating a RangeError: In this example we are checking that a
defined age is lower than 100, and we throw a new RangeError with
our own custom message:
var _age = 120;
try
{
if(_age > 100)
{
throw new RangeError("Age cannot be over 100");
}
}
c)
Generating a ReferenceError: A reference error is the result of,
in this case, trying to access a variable or method that is not defined.
Observe though that when running this code, you will see a TypeError instead.
That is the default browser behavior, as follows:
try
{
// use an undeclared variable
trying.thisone;
// TypeError due to browser
}
d) Generating an EvalError: An EvalError, while not a common one, is the result
of using the eval function in an unsupported manner. In this case, we use it
as a declaration. Again, due to browser-specific interpretation, the actual
error returned is a TypeError:
try
{
var y = new eval();
// TypeError due to browser
}
e)
Generating a SyntaxError: Contrary to the name of this error, it does not have
anything to do with improper syntax. When encountering improper syntax, the error
you will see will be more along the lines of the following screenshot:
A SyntaxError is the result of improper use of the eval() function. In our example
we are evaluating the sum of a string with an integer, thus resulting in a syntax
error:
try
{
var _x = "some string";
var _y = 10;
var _total = eval(_x + _y);
}