There are a number of blogs out there with good information on how to include external javascript files in your CRM OnLoad events. I’m only adding this post to the mix to bring together some of the different information out there for easy reference.
CRM provides three primary places to add client-side code: OnLoad and the OnSave for the form, and OnChange for individual fields. In the form editor for an entity, OnLoad and OnSave scripts can be added to the form properties. Each field has its own properties area where OnChange scripts can be added.
For easier management of client-side code, we often use just the OnLoad area to store all the javascript we add for a particular form. We’ll define the functions we want to use throughout the form, and call them from field or form events. You can use attachEvent to hook into events on individual fields so you don’t need to add javascript behind every single field. An attachEvent works like this (notice my function is calling the FireOnChange method as well):
/* define your function */
function MyFunction(){
crmForm.all.myfieldname.FireOnChange();}/* attach your function to the onclick event
crmForm.all.myfieldname.attachEvent('onclick', MyFunction, false);
Check out Mitch Milam’s blog at http://blogs.infinite-x.net/2009/04/20/changing-a-checkbox-field/ for a look at how he used attachEvent.
Taking this approach a step further, it sometimes makes sense to store the javascript in an external .js file. In that case, the form’s OnLoad properties will just contain a reference to an external file, sort of like an ‘include’ statement that appends our external .js file as the form loads in the browser. Here’s an example from http://www.henrycordes.nl/post/2008/05/External-js-file-and-CRM.aspx:
/* Place in the OnLoad area of a form */function IncludeExternalScript(scriptFile)
{
var netRequest = new ActiveXObject("Msxml2.XMLHTTP");
netRequest.open("GET", scriptFile, false);
netRequest.send(null);
eval(netRequest.responseText);
}
/* There is an ISV folder in the CRM webroot */
/* that your filepath should point to */
IncludeExternalScript('/ISV/_customscript/customscript.js');
CallFunctionInCustomScriptjs();
One thing to be aware of with this approach is that if you set up any users with the full Outlook client that allows them to work with CRM offline, all your calls to external script files will fail when they are offline since the file is on the CRM server. You can include a check before you add the external script and then do something like disable all the form’s fields if they are offline, but this starts to defeat the purpose of using the external file to start with.
Also, there are various methods of attaching external scripts. This method seems to work well, but you should be aware that the file will be cached in the browser, so if they make a change, users may not get the changes right away. One way around this is to go into IIS and navigate to the ISV folder and add a custom header to the external file to control caching. More info on this is at http://ronaldlemmen.blogspot.com/2008/07/javascript-files-and-caching.html.
1 comments:
Nice job.
Post a Comment