Chris76786777
Chris76786777

Reputation: 739

My CRM 2011 web resources will not load unless IE developer console is open

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 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);
}

Upvotes: 0

Views: 3991

Answers (2)

beluga
beluga

Reputation: 836

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_ };

Upvotes: 3

Seer
Seer

Reputation: 5237

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));

Upvotes: 2

Related Questions