Thursday, April 29, 2010

How to use External Javascript in your form

Just got back from the Convergence 2010 conference, and learned a LOT of great information on customizing Dynamics CRM.  One of this things I found very suprising was that the consulting services people said that it was OK to use external javascript files in your forms to make development easier.  I always thought this was unsupported in CRM, so I was happy to hear this.  
NOTE: If you have Outlook client users that take CRM offline, this method will not work.  You will need to come up with a way to get the external files onto their PC.  I have no idea how to do this, sorry.


Clicking Settings/Customization/Customize Entites/Case/Forms and Views/Form/Form Properties/OnLoad/Edit then pasting in the new code I just edited in VS2008.  Click Ok/Ok/Save and Close/Actions/Publish.  That's a lot of steps. :)

So when I got back to the office, I set to work externalizing the HUGE javascript files I had already created.  I found several other blog posts on how to do this, and frankly am a little frustrated that there are apparently several methods to do this, some of which don't work that well.

The big problem is caching in the browser.  A good idea when you want the browser to be fast, but a bad idea and a big pain when you are developing and constantly editing code, saving, and testing.

So here is the final code that I pasted into the OnLoad for my case form.

function load_script(url) {

   var url2 = url + "?dummy=" + Math.random().toString().substring(2);
   var x = new ActiveXObject("Msxml2.XMLHTTP");
   x.open('GET', url2, false);
   x.send('');
   eval(x.responseText);
   var s = x.responseText.split(/\n/);
   var r = /^function\s*([a-z_]+)/i;
   for (var i = 0; i < s.length; i++) {
     var m = r.exec(s[i]);
     if (m != null) {
        window[m[1]] = eval(m[1]);
     }
  }
}

load_script("/ISV/_scripts/CaseForm.js");

I won't pretend to know exactly what this script is doing, I just compiled it from a bunch of other sites and blogs.  The twist that I added was the editing of the URL to add the random math string.  Again, not my idea, just mashed it up from posts on these three sites.

http://www.henrycordes.nl
https://community.dynamics.com/blogs/crmmattwittemann
http://social.microsoft.com/Forums/en-US/crmdevelopment

Also, want to give a shout out to Julie Yack for inspiring me to start a blog.  I attended a Social Media interactive discussion she was facilitating at Convergence 2010.  If you find this blog useful, check out her blog as well.

http://julieyack.blogs.com/

She worked on a fantastic book that I refer to frequently when working on my CRM project, CRM as Rapid Development Platform.

1 comment:

  1. Welcome Chris. Feel free to point to the CRM Team blog too at http://blogs.msdn.com/crm.

    ReplyDelete