While the basics of any good development practice include error trapping and handling, when using JavaScript with Dynamics CRM 2011 your options revolve around using the try/catch block in a creative way. But the standard catch block has some additional features you should be taking advantage of when needed.
For this exercise we will be either using an existing solution or creating a new one. We will be creating a script that shows all available error-handling options available to us when using JavaScript with Dynamics CRM.
For advanced error handling perform the following steps:
new_errorhandling
). Leave the standard Yes and No options.new_JSErrorHandling
).function myErrorHandling() { // put in code that generates an error // change this code to throw various error types // and see how the errors are being captured try { throw new URIError("This is a URIError thrown..."); // throw new RangeError("This is a RangeError thrown..."); // throw new TypeError("This is a TypeError thrown..."); // throw new SyntaxError("This is a SyntaxError thrown..."); // throw new Error("This is a Custom Error thrown..."); } catch(err) { switch(err.name) { case "URIError": alert(err.name + " || " + err.message); break; case "RangeError": alert(err.name + " || " + err.message); case "TypeError": alert(err.name + " || " + err.message); break; case "SyntaxError": alert(err.name + " || " + err.message); break; case "CustomError": alert(err.name + " || " + err.message); break; } } finally { alert("This block executes at all times"); } }
OnChange
event of our JS Error Handling form field.This function processes a block of code that is meant to throw an error. The type of error in our example is the standard-type error, but you can change the kind of error thrown by looking at the Handling unexpected processing recipe. We are catching various types of errors that JavaScript can return, and performing various functions based on that. For this example, we are only returning different alert messages, but you can decide to build additional code to massage the form or process additional calculations as needed. In the end, we add a final block, which will execute no matter what kind of error we captured. This demonstrates ways of identifying the type of error generated by your code, and decide which way to handle it.
For the sake of simplicity our function does not check the value of the field that generates the OnChange
event.
3.142.54.239