Joe Levi:
a cross-discipline, multi-dimensional problem solver who thinks outside the box – but within reality™

Custom JavaScript in Microsoft Dynamics CRM 4.0

We do a lot of customizations on our Microsoft Dynamics CRM 4.0 deployment, specifically, adding buttons that run custom reports based on data provided in the CRM form (custom price quoting, etc.).

We accomplish this by modifying the isvconfig.xml file to add buttons that call JavaScript functions. For the purpose of this article I’ll assume you know how to add buttons to various forms via the XML.

Adding a JavaScript onClick event is fairly easy to add to your custom button, but wiring up the event isn’t quite as easy — unless you know this trick.

The issue at hand is that the form properties only exposes a text block for the form_onload() function, which it opens and closes outside the editable form.

Simply adding your new function inside this text block will cause an "error on page" condition and the button’s click event will fail.

To fix this we need to put the custom function(s) outside the form_onload() function, but we have to close the form_onload() function to do this. Also, we must insert a dummy function at the end of our customizations to properly close the curly braces that we prematurely closed when we closed the form_onload() function. Below is a code sample of the needed code. Please note this, by itself, is not valid javascript, but when inserted by the CRM engine, it is valid.

   1: // End the stock form_onload() function
   2: }catch(e){displayError("window", "onload", e.description);}}}
   3:  
   4: function yourCustomFunction()
   5: {
   6:     var yourParameter = "some-parameter";
   7:     var url = "";
   8:  
   9:     url = "http://yourlink.com/" + yourParameter;
  10:  
  11:     if (url != "")
  12:     {
  13:         var params = 'width=940, height=580, top=30, left=30, menubar=yes,titlebar=yes,toolbar=yes,status=yes, resizable=yes';
  14:         window.open(url,null,params);
  15:     }
  16:  
  17:     // The following is needed to properly close what w
as
 left over when we
  18:     // closed function_onload() in line 2.
  19:     function msft_dummy_function()
  20:     {{try{

You can thank me later.&trade

Share

You may also like...

1 Response

  1. xa says:

    Thanks for the post.
    you have an extra curly brace ‘{‘ before the ‘try’ statement.

Leave a Reply