CRM 2013 – Using Dynamic Javascript

Javascript is a slippery beast, Slave to no one and master to many.   It’s just a quick blog post today, not as quick as this article I found written by me

CRM 2011 – Javascript snippet – How to remove all existing values from an OptionSet


I’m more happy in the .net and C# world with it’s static variables.

What I mean by this is C# you have to declare the variable you are going to use e.g.

  • string
  • int
  • bool
  • custom type


but with Javascript a var can be anything, it’s like something out of X-men changing and morphing it’s way along.

Here is a good article about static and dynamic languages

Static vs. dynamic typing of programming languages


Why doesn’t Hosk like Dynamic Javascript

Good question, surely I should like dynamic things.

Some of the reasons I don’t like it are

  • Hard to read/understand other peoples code
  • Bugs can be compiled and then appear in runtime when the code is run
  • It encourages the cowboy coders to go to town
  • It’s strange and different


Get to the point

I’m not saying I don’t like Javascript it’s just sometimes I want to do things and I’m not sure HOW because the syntax can be a bit crazy.  Like all developers once we have a working example in our hands, we are off.  So recently I saw some interesting Javascript in a project I am working and I was intrigued to understand how it worked and use it again.

One cool feature about Javascript is the ability to pass in function names using variables, a bit like reflection(ish), but only once you know how because I tried searching for this on the internet and fell down a big dark hole.



On a form I have 4 OptionSets which we are dynamically setting the values to.

If I pass in the OptionSetField name I can dynamically get the control

var control = Xrm .Page .getControl (OptionSetFieldName);

I can then clear this control

control. clearOptions();

I can then add in the new values and build it up.  The beauty of dynamic code is I can use the same code for all the OptionSets because I can pass in the name of the control.

When adding a the new optionSet values in, I can use a dynamic variable to retrieve the field name from array, which contains values retrieved from a fetchXML query.  The childField is the name of the field in CRM.

      var option = {
                    value: retrievedOptionSet[i]. text == "" ? "null" : retrievedOptionSet[0].attributes [childField]. value,
                    text: retrievedOptionSet[0].attributes [childField]. formattedValue
                control. addOption(option);


It may be common knowledge to all you JavaScript wizards out there, but I hadn’t used or seen JavaScript using dynamic names.  It makes sense and it’s very useful so I thought I would share it with you



3 thoughts on “CRM 2013 – Using Dynamic Javascript

  1. ukcrmguru November 24, 2014 / 8:25 pm

    This looks like a pretty interesting technique – thanks for introducing me to this as a new idea. I was not aware of this. Not sure how often I would use it, but one to have “in the toolbox”.


  2. Joe Newstrom June 15, 2016 / 5:05 pm

    Ben, have you investigated simpleXrm.js?

    I built it specifically to address many of these frustrations. It is naturally still bound by the limitations of the language and Microsoft’s APIs, but CRM JavaScript is given a heaping helping of syntactic sugar, encapsulation, and normalization.

    It allows you to more easily read your code and your colleagues ‘, and also collapses the complex API plumbing into a library layer that can be updated on a per-organization basis to handle breaking changes in Microsoft’s APIs.

    Documentation is my biggest hole right now. The feature set is rich but like most FOSS it’s a time consuming hobby, and I have only so many hours in the day.

    Anyway I think you might appreciate the effort since you obviously have encountered many of the same pain points with JavaScript in CRM that I have.




Leave a Reply

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

You are commenting using your 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.