XRM 2011 JavaScript: Another 101 Lesson in Microsoft Dynamics CRM 2011

When you’re learning JavaScript, you’ll often come across the need to do something to every field on the form.  When you do, it’s important to have efficient code to make such widespread changes so the user doesn’t have to wait for the JavaScript to finish.  This lesson consists of several parts, some of which you’ll probably already know but hopefully there are some things in here for everyone.

Setting up our Environment

First thing first, open your CRM 2011 development environment and browse to an account form.  Once the account form is open, hit F12 on your keyboard.  A window should popup that looks like this:


If instead, you see the window in the bottom of your browser page.  Click the little “Unpin” button shown here:



This is IE Developer Tools and if you haven’t used this in the past, then I hope you enjoy not having to save/publish/refresh/test nearly as often.  To start, we’re going to see how many fields we have on the form.  When inside of the CRM form, we’d use something like:


But when we’re inside Developer Tools, we actually have a different context.  So we need to click on the “Console” tab and execute:




Anything we want to execute on the form, we can do by simply prepending “frames[0].” in Developer Tools to get the proper context.  This allows us to write and test code instantaneously, which is a huge timesaver.

Iterating Fields

Now that we are playing in our Developer Tools sandbox, let’s iterate through some fields to see which fields are required.  To use multiple lines, click the double up arrow in the bottom right corner.

Here’s some slightly modified code from the SDK that we can just copy and paste into our Console:

Note: I can’t stress enough how awesome the SDK is. Everyone I’ve met who partakes in the SDK creation, maintenance, etc. have been top notch people. If you aren’t using the SDK, you are missing out in tons of awesome tips, features and customizations.

You’ll notice that the above code quickly goes through each field and checks to see if the field is required.  We could also check if the field is dirty (meaning the user changed the value), if the field is enabled, or whatever.  It’s just a simple way to make mass changes quickly.

About “forEach”

For the experienced developers, you may be thinking did IE get ForEach?  No, but the CRM developers were gracious enough to add this into the Xrm variable.  For those of you unfamiliar, this was an old Mozilla thing that made life a lot easier when dealing with arrays.  Expanding further, the Xrm forEach approach is faster than most arrays people would generate using for loops.  You can write some JScript that is faster and I’ll get into that one day, but at this point you are better off using the forEach approach.

I’ve said it several times, but I really like the Xrm object due to its attention to performance.  In fact the method here is about a hundred times faster than iterating the crmForm.all object.  I’ll back that up with some pretty graphs and such in a later post.


Checking for Changed (Dirty) Fields

Notice how easy it is to change what we are checking for.  Whether we are looking for a specific value, dirty fields, required fields, or whatever, doing so inside the forEach function makes it fast and easy.


Back to the Web Resource

To go from the Developer Tools back to the Web Resources, we need to replace all of the “frames[0].” references with blanks.  IE Developer Tools is a great way to create and test our code without affecting anyone else and there is a lot more to developer tools than the console.  I hope you enjoy!

Share this Story

About Paul Way

Born and raised in Greenville, SC. I'm a coder to the core. The only thing I love more than coding is my family and your epic comments.

Leave a Reply

Your email address will not be published. Required fields are marked *


© Copyright 2012-2014, All Rights Reserved