How to find Javascript files used in Microsoft Dynamics Forms
Sometimes when you work on a CRM project which contains lots of Javascript files, it can be difficult to determine what Javascript files are being used and which ones are still needed.
The route cause of this is Microsoft Dynamics CRM forms can have lot of methods triggered from fields or ribbon buttons but these can be difficult to find to find what Javascript files are being used.
Where is Javascript triggered in CRM
Javascript files can be used on CRM forms and the code can be triggered on forms loading/saving or by the OnChange event of a field and the Ribbon buttons. Below is where Javascript files can be used in CRM
- Javascript triggered by OnChange events
- Javascript triggered by Ribbon buttons
- Javascript triggered in form OnLoad/OnSave events
- Javascript files used in html pages
There are many different areas Javascript can be used but it can be difficult to see what Javascript files are used and linked to field OnChanges, ribbon buttons.
One of the reasons this is difficult is because the CRM UI means it’s not easy to see what fields have Javascript functions triggered via the OnChange event and the only way is to either look at the Javascript files and hope the CRM Developer has used a logical naming structure.
or
You can click the fields in the CRM form and check if there is an OnChange event.
Javascript development and deployment
Javascript development and Javascript deployment can be a bit disjointed. One method is to write the Javascript file in Visual studio then you copy the Javascript file and paste it into the CRM form changes, save and publish.
It should be noted this isn’t a new problem for CRM 2015/2013 , there have been lots of tools created to help the process.
CRM MVP Mitch Milam created the export Javascript file tool, which I hada used in older versions of Microsoft Dynamics CRM, which you can read about here
The Microsoft Developer toolkit brought big improvements which allowed you to deploy/update Javascript file changes from Visual studio. I go through the process in this video from Hosk CRM Dev Youtube channel
What Javascript files are used
I recently worked on a project which had gone through some optimization and refactoring, my current task is finding out if all the Javascript files are needed for each of the forms.
The reason the project is in this state is we quickly needed to optimize the code and whilst doing this the Javascript was optimized and refactored, this has resulted in some of the Javascript files not being needed
The code was refactored but some of the Javascript files which were no longer used were not removed.
The code was also part of the Async Javascript load issue, which happened in CRM 2011 Rollup 12
CRM 2011 – Things learnt when reviewing Javascript code on form loads
Here is my blog post on CRM form performance
CRM 2011/2013 Investigating CRM Form Performance issues
The Problem
Tidy up Javascript files on forms, remove the WaitForScriptsLoaded Javascript file and replace the onload method with the entity Javascript file.
Remove Javascript files which are no longer referenced.
Factors
To make the problem harder, the Javascript was split over 10 different Javascript projects, which made find if Javascript files were used
To help me the project adhere’s to Javascript best practices and uses Javascript namespaces.
link to Javascript namespaces
Things to consider
When looking at what Javascript files are needed don’t forget to check the Javascript files used for the ribbon buttons, in this particular project most forms had 5 custom ribbon buttons.
Tools used
Fiddler
I used Fiddler to track what calls were being made out of the form
Visual Studio
I used Visual studio to look at the Javascript files included on the FORM.
F12 Debugger
Press F12 on a record and I could see all the Javascript files being used.
Script finder tool
I knew there was a script finder tool but I couldn’t find it when searching the internet. Thankfully a reader added a comment and of course it’s in the everyone’s favourite CRM tool or collection of Tools the XRMToolbox.
The XRMToolbox has a Script finder tool which will list all the Javascripts and will show the event e.g. OnLoad, OnSave, OnChange as you can see from the screenshot below
The tool looks great but I would say the project I was working on has a lot of Javascript and when I tried I pressed the find scripts button it never came back. I think it timed out because there are loads (hundreds) of scripts and functions.
Process of finding unused Javascript files
I did try doing a find in files but this proved to be very slow and seemed difficult, I eventually decided this process wasn’t really working.
So I looked at the problem a different way
How do I find what files are used when the CRM form is being used?
I came up with two main methods.
Method 1
Go through the main Javascipt file specified in the form onSave/OnLoad and look what other Javascript files/methods are referred. Thankfully the Javascript files used were developed with using Javascript Namespaces so I could search for the namespaces in the file to see what other Javascript files were used.
I cold then compare the Javascript files listed in the Javascript files for the form and what Javascript files were referenced in those Javascript files.
Method 2
When the Javascript form is being used in CRM you can press F12 to get the debugger up, I could see what Javascript files are loaded.
Why did this happen
Good CRM Development will mean the CRM developer will create lots of seperate Javascript files which can used in multiple CRM forms. The reason you split the Javascript file into common Javascript files is because it’s better to use the same Javsacript code in many different forms rather than copy the code in many different Javascript files (e.g. if you have one Javascript file for each form).
You never want to duplicate code because if there is a bug in the code then you would have to fix all the various copies of the code and the chances are you would forget to change some of them. Later it would be difficult for the CRM developer to know which code was fixed and which code still had the bug in.
When Javascript code is no longer needed CRM developers often forget to remove Javascript files and methods, this can add to the confusion for a CRM developer when there are javascript files and methods which don’t seem to be used. It can be scary to be the CRM developer to remove unused Javascript files and methods because it’s difficult to be 100 percent sure they are not used anywhere.
Filed under: CRM 2011, CRM 2013, Hosk CRM Dev, Hosk’s Microsoft Dynamic CRM Development, Javascript, JScript

*This post is locked for comments