Thursday, April 18, 2013

Error Handling in MSCRM 2011 Javascript




 
In addition to these error types, catching default syntax errors can be done by using the
standard
onerror event of the window. The syntax for that is as follows:
window.onerror = function(parameters){
// processing
}
Add this to the script section of the head.
To handle unexpected processing, perform the following steps:
1. Open the
Contact entity's main form for editing.
2. Add a new option set named
Errors (new_errors), with options such as URIError,
RangeError
, ReferenceError, EvalError, TypeError, SyntaxError, and
CustomError
.
3.
Add the field to your form.
4. Save and publish the form.
5. Create a new JScript web resource in your solution. Name it
JS Errors
(
new_jserrors).
6. Add the following function to your resource:
function ErrorHandler()
{
var _error = Xrm.Page.getAttribute("new_errors").
getSelectedOption().text;
// alert("Selected option is: " + _error);
switch(_error)
{
case "URIError":
try
{
decodeURIComponent("%");
}
catch(err)
{


alert(err.name + " || " + err.message);
}
break;
case "RangeError":
var _age = 120;
try
{
if(_age > 100)
{
throw new RangeError("Age cannot be over 100");
}
}
catch(err)
{
alert(err.name + " || " + err.message);
}
break;
case "ReferenceError":
try
{
// use an undeclared variable
trying.thisone;
// TypeError due to browser
}
catch(err)
{
alert(err.name + " || " + err.message);
}
break;
case "EvalError":
// not used in recent versions, but supported
try
{
var y = new eval();
// TypeError due to browser
}
catch(err)
{
alert(err.name + " || " + err.message);
}
break;
case "TypeError":
try


{
var _obj = {};
// call undefined method
_obj.execute();
}
catch(err)
{
alert(err.name + " || " + err.message);
}
break;
case "SyntaxError":
try
{
var _x = "some string";
var _y = 10;
var _total = eval(_x + _y);
}
catch(err)
{
alert(err.name + " || " + err.message);
}
break;
case "CustomError":
try
{
throw new Error("Custom Error message");
}
catch(err)
{
alert(err.name + " || " + err.message);
}
break;
default:
// do nothing
}
}
7. Associate the
ErrorHandler function to the OnChange event of the field we
created.
8. Save and publish your solution.
9. Test the function by changing the values in the option set.
 



 

No comments:

Post a Comment