Referencing an External Jscript from an OnLoad Event in Microsoft CRM
Disclaimer: TOTALLY UNSUPPORTED.
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
src=‘https://<server>/<site>/<filename>.js’
language=’JavaScript’>”);
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
- Anonymous
February 23, 2006
What manual do you mean? Where can you buy it? - Anonymous
February 27, 2006
It is the training manual... If you are a partner, you can buy them from PartnerSource. https://mbs.microsoft.com/partnersource/communities/training/trainingmaterials/default.htm If your not a partner, find one and buy them off them... - Anonymous
February 27, 2006
The comment has been removed - Anonymous
February 27, 2006
Paul,
Now doing that would be totally unsupported... :-) - Anonymous
February 28, 2006
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 ;-) - Anonymous
March 01, 2006
Cool, I like it. Keep up the good work. - Anonymous
March 01, 2006
Thanks Rick... :-) - Anonymous
March 19, 2006
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. - Anonymous
March 19, 2006
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.... :-) - Anonymous
July 27, 2006
Having to write all of your custom CRM script 'inline' inside the event blocks of form/field events has... - Anonymous
September 15, 2007
Thanks, that works great. It’s pretty crafty. I haven’t considered using/referencing an external jscript file. This can be a nice convenience.