Exception Handling Tips for JavaScript in MS CRM 2011 Ribbon

When there is an error in JavaScript code of the event handlers on CRM forms, there is an alert for the error message.

But when there is an error in JavaScript code used in EnableRule or Actions of CRM ribbon buttons, there is no error message showing and we can only see the form just loading partially, "Error on page" showing at the status bar of the window, or sometimes just nothing happen on button click.

It becomes a big problem when the developer update the codes here and there in a lot of functions without updating and testing bits by bits. If we find out there is an error after updating multiple functions and deployed at once, we would not be able to pinpoint the source of the error without any error message.

So, it is best practice to do exception handling for each and every functions used in EnableRule or Actions of CRM ribbon buttons as in the example below.


 function EnableDisableTestButton()  
 {  
      try  
      {  
           // Button custom rule checking  
      }  
      catch(err)  
      {  
           alert("Error in EnableDisableTestButton function. Error Message: " + err.message);  
           return false;  
      }  
 }  

 function OnClickTestButton()  
 {  
      try  
      {  
           // Button click event  
      }  
      catch(err)  
      {  
           alert("Error in OnClickTestButton function. Error Message: " + err.message);  
           return;  
      }  
 }  

If there is no error message even with those exception handling, it is most likely that there is a syntax error in the code. You can check the errors in your JavaScript code at the JavaScript Lint in the following URL.
http://www.javascriptlint.com/online_lint.php

Comments

Popular Posts