Include Javascript files in CRM
This must be familiar to most of you: you're making quite some modifications to the onchange event of a picklist. To keep this readable, you do use tabs and breaks, but after saving this, all tabs and enters are gone. Therefore I like to put most code into .js files and include these files in the page. This would break the contract tough. Another possibility is the httpmodules, but they require quite some effort.
Now I was searching for some information regarding Javascript and I found some interesting codes. One of these could be used to solve the problem as described above! What about loading javascript files WITH javascript? It is possible by using this script:
var script = document.createElement('script');
script.type = 'text/javascript'; script.src = 'code.js';
document.getElementsByTagName('head')[0].appendChild(script);
When putting this code into the onchange event, this stays readable, does not cost much effort and you wont break the support contract!
Edit:
The script above could give troubles when a function call is made to the javascript file when it is not yet fully loaded. Thanks to Alex for providing this script:
function OnChange()
{
var script = document.createElement('script');
script.language = 'javascript';
script.src = 'MyFunctions.js';
document.getElementsByTagName('head')[0].appendChild(script);
var f = function()
{
if (event.srcElement.readyState == "loaded")
SomeFunction(); // some function from MyFunctions.js
}
script.attachEvent("onreadystatechange", f);
}
// function OnChange()
Update:
Just a bit better readable script:
var script = document.createElement('script');
script.language = 'javascript';
script.src = '/Custom/JavaScript/opportunityOnloadOnsave.js';
script.onreadystatechange = OnScriptReadyState;
document.getElementsByTagName('head')[0].appendChild(script);
function OnScriptReadyState()
{
if (event.srcElement.readyState == "complete")
{
//perform onload scripts
CustomOnLoad(); // some function from your custom js file
}
}

2 comments:
Hi! I would like to ask if there is a solution that works also for outlook clients. I mean: exists a directory where i could put my javascripts so that they are also available (get synchronized or whatever) by outlook clients without any additional effort for me?
Hey Ronald,
could you tell me script to retrieve entity id (not the one on which javascript gets triggered) usine REST endpoint.
To explain it in better words, I want to retrieve Records of a custom entity and perform some condition operations on them and update current contact record.
Thanks in advance
Post a Comment