Question

I have devised the JavaScript logic as per module pattern for my CRM 2011 form. At the time of form load sometimes i get the alert as 'Crm object undefined' but upon refreshes (single or multiple) this error disappears (work smoothly).

After quite struggling I am still not able to understand that why this happens. Has it anything to do with module pattern? because onLoad function is fired just fine but the thing that is bothering me is that why sometimes Crm object is not created.

var Crm = (function (FSX) {
    var xrm = {
        date: {
            setDate: function (sourceAttribute, targetAttribute) {
                // ... date setting logic ...
            }
        }
    }
    return xrm.date;
}(FSX));

function onLoad() {
    if (typeof Crm === 'undefined') {
        alert('Crm object undefined');
        return;
    }
    Crm.setDate("fsx_source", "fsx_target");
}
Was it helpful?

Solution

I think the problem lies with the FSX object. Where is this loaded?

If it has not been loaded before the script defining your module executes, the code will error out and Crm will be undefined.

There is a nicely written article here about loading dependant libraries for a CRM form.

It proposes are a number of solutions: 1) Use RequireJs or HeadJs to dynamically load your dependant scripts. 2) Loading Libraries by including them in Ribbon commands.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top