Creating custom JScript for Microsoft Dynamics CRM2011


I’m in the process of creating some custom JScript code to have some fields on an expense report form auto calculate and to enable one textbox if a certain expense type is selected.

I’ve been at this all day and I have been getting errors along the lines of “Error: Object expected” thrown at me from Dynamics when testing on the form.

It turns out the website I was using an example from used the quote characters “ ” instead of ” “.

It took me close to 4 hours to figure out why my ENTIRE SCRIPT LIBRARY was broken because of this when I was following some online examples exactly.

Yes that’s right my entire script library broke because one function had improper quotes and therefor a field object wasn’t being returned even though the function wasn’t being called it broke every other function in the library.

So word to the wise if you get an Error: Object expected in Dynamics CRM2011 check the following things:

  1. Is your field name spelled EXACTLY right including case? my_fieldname is not the same as my_FieldName
  2. Are there any spaces added by mistake in the name?
  3. Are you using the entity field name or the display name? entity name is correct EX: new_mycustomfield not “My Custom Field Label”
  4. Reduce down your JScript library and check for missing semi colons or other functions that may be broken to isolate the proper broken function it may not be where you think it is.
  5. Finally did you copy paste an example from another website? if so check the quotes to make sure they are the proper double quote character it should be ” “ not “ ”
EX: Xrm.Page.getAttribute("new_mycustomfield"); not Xrm.Page.getAttribute(new_mycustomfield);

As a side note always remember to add code to the onSave event handler for the form as well as the onChange handler for a field if you are using read only fields you need to forceSubmit those values to save them.

EX: Xrm.Page.getAttribute("new_mycustomfield").setSubmitMode("always");