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.


Comments (11)

  1. David Pitkin says:

    What manual do you mean? Where can you buy it?

  2. Ben Vollmer says:

    It is the training manual… If you are a partner, you can buy them from PartnerSource. If your not a partner, find one and buy them off them…

  3. Ben,

    Just as a side note to this, if anyone is doing this on a production environment; they MUST give permissions to the JS file to the IIS_WGP and the other IIS users the ability to read and execute this file or else anyone other than the admin will see errors.



  4. Ben Vollmer says:


    Now doing that would be totally unsupported… 🙂

  5. True, but if someone wanted to be "unsupported" but still have their system function, that would be the way to have it work. Not that I think people should have unsupported customizations… that would be bad 😉

  6. Rick Harris says:

    Cool, I like it.  Keep up the good work.

  7. Ben Vollmer says:

    Thanks Rick… 🙂

  8. Jim Daly says:

    Just want to point out that the Customization Manual presents this as a development practice and lists several reasons why this may not be a good approach for a production environment. For example – It won’t work using the Laptop client for Outlook.

    This technique can help you develop scripts faster, but as a final step you should take the logic from the external script and paste it into the onLoad event and remove the code that does the script injection. This way you have all your code in the metadata and you will return to a fully supportable scenario.

    You can also define all your event logic as functions in the onLoad event – and just include a function call in the onSave and OnChange events. This keeps all your code in one place to make it easier to manage.

    If you want to call a function in the onLoad event, make sure you define the function first – then call the function at the bottom of the onload event script.

  9. Ben Vollmer says:

    Thanks Jim… Hopefully you have seen an uptick in orders for this manual… I think EVERY consultant should have a copy… I use it almost every week…. 🙂

  10. Having to write all of your custom CRM script ‘inline’ inside the event blocks of form/field events has…

  11. banhinc says:

    Thanks, that works great.  It’s pretty crafty.  I haven’t considered using/referencing an external jscript file.  This can be a nice convenience.

Skip to main content