Advanced error handling

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.

Getting ready

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.

How to do it...

For advanced error handling perform the following steps:

  1. Open your existing solution or create a new one.
  2. Add the Contact entity to your solution.
  3. Create a new Two Options field named Error Handling (new_errorhandling). Leave the standard Yes and No options.
  4. Add the field to the form.
  5. Save and publish the form.
  6. Create a new JScript web resource named JS Error Handling (new_JSErrorHandling).
  7. Add the following function to your script resource:
    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");
      }
    }
  8. Save and publish the resource.
  9. Associate the function we just created to the OnChange event of our JS Error Handling form field.
  10. Save and publish your solution.
  11. Test the solution by setting the value of JS Error Handling to Yes. The script will execute, and will prompt alerts based on the type of error encountered, as well as a final message generated by the block.
  12. Change the main block of code to generate other type of errors as described in a previous recipe, and observe the various errors handled in this function.

How it works...

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.54.239