When you start working with Javascript there are some distinct disadvantages to using the web-interface which is built into CRM.
a) The interface doesn't have IntelliSense
b) It doesn't colour your code or indent it to make it easier to read
c) It is just too much trouble to attempt to develop code and debug it within the web interface.
Personally, I have a code editor which I rather like called HTML-Kit and I have been developing my code in there and then just doing a straight cut and paste into the window to test it, but even this gets rather tedious after a while. So, here is a better way....
Instead of pasting each individual function into an event, what if you could paste one generic function that only needed about 30 seconds of tweaking?
Better still, you would have all of your custom functions in one place, one file and all properly commented and sorted for later reference and ease of manipulation. Well, here's how.
1) Set up a new directory under IIS called (say) Custom_Javascript
2) Open a new file called (say) Custom_Code.js
3) Paste the following code into Custom_Code.js and save it to your new directory.
function CallTestMe()
{
alert("I work!");
}
4) Now, go to the onLoad event of any Form in any entity you like, for sake of argument, I'll use a custom entity of my own for this demo, just so long as you can trigger the event by creating a new record.
5) Paste the following code into the OnLoad event of that form
var oXML = new XMLHttpRequest();
oXML.open('GET', "/Custom_Javascript/Custom_Code.js", false);
oXML.send('');
eval(oXML.responseText);
CallTestThis();
6) Now, go to your Entity and trigger the OnLoad event and you should see....
Voila!
The implications of this method are rather more far reaching than a single alert box.
1) First of all, you have all your custom code in one place, you dont have to remember where you put it and on which entity, although, I have created a custom entity for myself which does record this information.
2) You can easily transport the file across networks for printing, editing or testing.
3) You just use a single function and change the call each time.
4) You can 'file' your different types of code within different js files according to function, project, entity etc.
And Finally...
Make sure you set the content expiration of the Custom_Javascript folder in IIS to expire immediately or the script will be cached by IE and you will not see changes as you make them.
To do this, go to the IIS manager on your server, find the directory in the list, right click and choose properties. You should see this screen...
Minor Warning : Although it is rather elegant to develop your scripts and to hold them in their own external directory, you should be aware that only the 'pointer' code will be exported with an entity. If this doesn't bother you, then this is a good way to keep code for entities segregated and filed. You should make sure that the Custom_Javascript directory is included in your regular backup schedule since the code will not be backed up with the CRM.
Recent Comments