The CRM Grid – A Microsoft CRM Blog

Greg Owens’ rose-tinted MS-CRM mumblings

More thoughts on managing your JavaScript code

Peter Maude made an interesting post, quite some time ago now, considering how one might manage the reams of JavaScript code that could potentially accompany a highly customised MS-CRM deployment.

I’ve pondered over this recently as I move from development to a live deployment and I migrate my custom JavaScripts from standalone .js files into the entity forms themselves. Presently I have some standard wannabe-global code for generic functions (such as show/hide elements of the UI), lots of entity-specific code in the onLoad event and often up to a dozen smaller script files which handle onChange events in various form controls.

Keen to make the effort to at least try and be in a supported environment, it strikes me that the simplest method is to put all of my code in the onLoad event handler and use the “attachEvent()” method to allocate functions to the appropriate form controls (thinking about it, this probably isn’t supported either…!). For example the code I currently use for each onChange event has its own .js file. I can simply copy this code, wrap it in an arbitrary function wrapper in the onLoad event handler and then attach that function as the onchange event for the field I want:

var fieldA_function = function exampleOnChangeFunction()
{
// function to be executed onChange of myfield A
alert('Field value is: ' + crmForm.all.new_myfielda.DataValue);
}
crmForm.all.new_myfielda.attachEvent("onchange", fieldA_function);

I haven’t tested this, but right now it feels logical enough. At the very least you will never have more script sources to manage as you have entities. I’d be interested to know your thoughts or ideas.

I’ve been wondering whether it might be possible to store code in a custom entity, e.g. called JavaScript and calling the code direct from the database… More on that when I’ve proven whether it’s possible or not!

Advertisements

4 Comments»

  Tom wrote @

Hi,

Above code works fine, saved me a lot of time, thanks.

Btw, your RSS feed points to a broken link – guess you just need to change the ‘mistercrm’ to ‘thecrmgrid’.

Cheers,

Tom

  Greg Owens wrote @

Hi Tom

Glad the snippet was of use to someone 🙂

Thanks for the heads-up on the RSS link too – certainly explains why Google had the old (temporary) URL and not the new one (doh!) !

Greg

  Andrew wrote @

Hi Greg,

Thanks for the post. It is working under the CRM4 RP0 VPC.
Now I am working to upgrade some my codes from CRM3. I need my codes working on the offline client. Your post helps me great.

Andrew

  Greg Owens wrote @

Thanks Andrew – good to hear it is still appropriate for v4 too 😉

Appreciate your feedback!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: