How to Resolve Microsoft Dynamics 365 jQuery Reference Issue

Adam Murchison, 17 June 2019

Have you ever had an error similar to the one below?

TypeError: Object expected
at Alert.$ (https:/xxxx/%7B636661942030000359%7D/WebResources/mag_/js/alert.js?ver=46925777:28:5)
at Alert.show (https://xxxx/%7B636661942030000359%7D/WebResources/mag_/js/alert.js?ver=46925777:60:9)

Are you adding in a jQuery reference to the form that this error is presented on? If so, the $ (jQuery) reference is conflicting on load of the form and there’s a discrepancy on which jQuery to be using. The $ within the client API wrapper is being set to the jQuery instance you are loading into the form, which isn’t necessary because CRM has its own jQuery in the parent context. The error occurs when the jQuery reference is set in the client API wrapper to an older version of jQuery and your script is trying to access it. There’s no need to be adding a jQuery file to CRM as you can simply access CRM’s jQuery reference which will always exist.

The above error is occurring using the Alert.js library which uses CRM’s parent context jQuery, where another addon has added its own jQuery library to the form which it is conflicting with.

The solution is to remove the jQuery reference from the form, this may cause issues as there’s no jQuery reference being automatically set in the client API wrapper. To stop this error, you can add the below two lines to your web resource and this will set the client API wrappers jQuery reference to the parent contexts jQuery. If you do not want to add this at the top of every web resource, create a new resource and add it to the top of the form libraries as shown in the image below. 

image

jqueryReference.js
var $ = window.$ || parent.$;
var jQuery = window.jQuery || parent.jQuery;


This sets the client API wrapper jQuery to CRM’s parent frame which will stop the conflict in versions of jQuery and ensure that it is always set.

Note that in Alert.js, this issue is also resolved in version 3.0.6.