0

MAJOR EDIT 1 Below is the description of my original issue but I've discovered that Chrome and Firefox are both loading the libraries correctly. It's IE 9/10 that will not work at all, even upon repeated refreshes. However, once I open the developer console and reload everything works fine.

I've read this post about the issue and followed the outlined steps. The problem is that jQuery is not reliably/consistently loading in IE 9/10, Chrome, or Firefox.

I'm also not able to, with 100% reliability, repeat the issue. However, what usually happens is that after I publish a change in my custom web resource, and reload the form, I will get "jQuery is undefined" in the developer console. Subsequent reloads will not display the error and there doesn't seem to be a difference in behavior if I clear the cache or not before reloading.

Here are some specifics of my implementation:

  • I'm using the CrmFetchKit library which depends on jQuery.
  • In an attempt to make this work I've combined the two libraries into one web resource. The thought behind this is that the error always comes from CrmFetchKit which says to me that that file has no issue with being properly included.
  • My code below.
  • My function 'genericFetch()' is set in the onChange event handler for a lookup attribute on my experimental form.
  • Everything works as expected (even if the js is super ugly) when jQuery is available.

I'm happy to provide more information and/or screenshots if requested.

// Fetch XML generic fetch format
function genericFetch(entity, targetField, returnFields, searchSource)
{
    returnFields = returnFields.split('|');
    var searchValue  = Xrm.Page.getAttribute(searchSource).getValue();

    if(searchValue)
    {
        searchValue = searchValue[0]['values'][1]['value'];
    }
    else
    {
        return false;
    }

    function onFetchError(xhr, status, errorThrown)
    {
        var errormsg = $(xhr.responseXML).find('Message').text();
        alert('CrmFetchKit-Error occured: ' +  errormsg);
    }
    var fetchXml = ['<fetch version="1.0" output-format="xml-platform" mapping="logical">'];
    fetchXml.push('<entity name="' + entity + '">');
    var len = returnFields.length;
    for(i=0; i<len; i++)
    {
        fetchXml.push('<attribute name="' + returnFields[i] + '" />');
    }
    fetchXml.push('<filter type="and">');
    fetchXml.push('<condition attribute="' + returnFields[0] + '" operator="eq" value="' + searchValue + '" />');
    fetchXml.push('</filter>');
    fetchXml.push('</entity>');
    fetchXml.push('</fetch>');
    fetchXml = fetchXml.join('');

    CrmFetchKit.Fetch(fetchXml).then(function (results) {
        /* success handler */
        console.log("results: " + JSON.stringify(results, null, 4));
        Xrm.Page.getAttribute(targetField).setValue(results[0]['attributes']['productnumber']['value']);
    }, onFetchError);
}
Community
  • 1
  • 1
Chris76786777
  • 709
  • 1
  • 13
  • 23

2 Answers2

3

This is due to this line of code in your script:

console.log("results: " + JSON.stringify(results, null, 4));

In IE the console object is undefined by default (this could be due to the fact that MS CRM is always in IE8 document mode, I'm not sure, but I have seen this problem on other sites).

As soon as you open the developer tools window, console object becomes defined, and the code starts to work.

My solution in all my scripts for MS CRM was: define an empty override for the console object if it is undefined, e.g.

var _f_ = function () { };
window.console = window.console || { log: _f_, error: _f_, info: _f_, debug: _f_, warn: _f_, trace: _f_, dir: _f_, dirxml: _f_, group: _f_, groupEnd: _f_, time: _f_, timeEnd: _f_, assert: _f_, profile: _f_ };
beluga
  • 826
  • 7
  • 14
  • I discovered this recently but completely forgot to update my post. Sorry, community! – Chris76786777 Feb 08 '13 at 23:48
  • Also happens in SharePoint, no errors or warnings about the console being undefined, but JSON will not work if you use a console statement – Dan Sep 18 '14 at 14:50
2

This could potentially be due to your script running before jQuery has loaded. i.e. if the script is not wrapped in:

$(document).ready( function() {
    // Your code...
});

or

$(window).load( function() {
    // Your code...
});

You may have more success with $(window).load(...); in your scenario.

This seems the most likely since it is an intermittent problem. However; I'd also suggest looking at whether or not your code has conflicts with another script, for instance, if a script that conflicts with jQuery loads first then it may stop jQuery from working properly.

You can solve that by using something like this:

(function( $ ) {
    // Put code using jQuery via '$' here...
}(jQuery));
Seer
  • 5,226
  • 5
  • 33
  • 55
  • I should have mentioned this (I always seem to leave some important detail out) but I actually removed the "document ready" clause because, for a reason beyond my understanding of JS, CRM would complain that genericFetch was undefined. (Actually, I suppose this makes sense because CRM doesn't natively understand jQuery.) – Chris76786777 Jan 31 '13 at 16:53