Referencing an External Jscript from an OnLoad Event in Microsoft CRM


So I have been spending a ton of time writing some JScript for the new VPC that we are producing right now. It is a complete pain in the neck to publish changes between troubleshooting and playing with JScript. Being the impatient guy I am, that extra 45 seconds just annoys me. :-)

So this idea from the Customization class (8525) in Chapter 6 made me fall in love with the manual. (There are a ton of other great scripts in that manual as well. I would purchase this one if I where you...)

Wouldn't it be nice to be able just to edit a .js file and test it as soon as you click save?

This method is valuable when developing scripts because it can reduce the amount of time you might spend saving and publishing the entity.

The Onload event enables the ability to inject an HTML<script> element to the head of an HTML document. This allows you to define functions in a separate JScript file and load that script with the page when the form loads. Because these functions are included when the form is loaded, you can call those functions from any form or field event for that entity. This way you can reduce the amount of JScript that you put into the Event Detail Properties window.

The entity does not need to be republished for any changes defined in the functions to take effect. You simply need to save the JScript file.

This code creates a new script element and inserts it into the head of the HTML document that defines the form:

st = document.createElement(“<script
h = document.getElementsByTagName(‘head’);
h[O] .insertAdjacentElement(’beforeEnd’,st)

Just replace the server, site and filename above with yours and it will work... :-)

When you have developed the logic you want, remove the code from the onLoad event and place the logic from the function of your .js file into the appropriate Event Details window. This will return your deployment to a supported state.