Javascript and Dynamics CRM 2011

I was asked to develop some CRM customizations using only Javascript.  As anyone who has worked with Javascript knows, it can be a bit of a hassle to work with, so I have compiled a list of hurdles that I had to leap over to reach my goals for this project in the hopes that it will help someone else (or if nothing else, give me a place to refer to when I have to do it again some day)

I installed CRMFetchKit, which makes it very simple to execute FetchXML queries and act on the results.  It’s easy to use the CRM “Advanced Find” feature to build the list of data you need and export the XML.  From here, you just need to paste it into code and insert your variables.

After the results are returned, the id field is extracted using the same field name that is used in the attribute elements of the XML query.  In this case, the name and id are used to populate a lookup, which gets populated differently than a standard CRM text field.  Brackets are required for setting the id fields of lookups.  The relevant code is below:







I was tasked to create a custom lookup on the Address entity, which doesn’t allow creation of lookups.  I had to create an HTML web resource and spend days messing with CSS and Javascript to get it to display and act like a CRM lookup.  Typically, Xrm.Page.getServerUrl() would give you the root of your CRM site, but in my case it cut off the server our CRM instance was running on, so I replaced it with the following:

This was used to link to the record when the name of the selected lookup value is clicked, as is the behavior of a standard CRM lookup.

For calling the actual lookup dialogue, I created a function to encapsulate the method call so that I could get intellisense when determining what fields I would fill in when I calling it.


Here is the code I used to call the lookup using the above function.  The State lookup should be filtered to show only the states that belong to the selected Country.  If it’s just a standard lookup, only the lookupStyle and lookupTypes parameters are required.

For another requirement, the client wanted “cloning” functionality for an entity, to be called from a ribbon button click.  When the ribbon button is clicked, a new window should open for creating a new entity and the relevant fields should be populated.   Installing the CRM Visaul Ribbon Editor makes it a snap to add ribbon buttons.  I don’t know any other way to do it, but this seems to be very easy.  So there are basically three steps to the process.

1. Collecting the field values from the source record. The fields need to be treated differently depending on the field type, as usual

2. Define the values that will be populated into the destination record

3. Open the new form, passing the values to be populated (AGREEMENT is the entity type name (in our case, ‘iis_agreement’)
















One last note – I used the trial version of JetBrains WebStorm for most of my Javascript development here, as it checks your variable names on the fly, whereas Visual Studio doesn’t.  So if you misspelled projectid in the code above (I did), you would have to wait until you run your test and use alerts or console.log writes to find out what went wrong. In WebStorm, there are a number of error checks you can enable, but you would notice it was the wrong color and fix your typo before it got that far.  Just trying to minimize your headaches, people.

Happy coding


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 )

Google+ photo

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