0

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

1 Answer 1

1

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.

3
  • FSX is creating in another script file that is loaded before this file. How is it possible that FSX is not created? You mean JavaScript can also behaves abnormally? Commented Sep 24, 2013 at 16:27
  • It's not Javascript. It has to do with how CRM is loading your scripts. Scripts are loaded asynchronously. The code above could be run before FSX has completely loaded. Try Loading the FSX script in the Ribbon. The ribbon scripts are the first to load and hopefully, your script will run after FSX has been created. The article I mentioned above has information on how you can do this.
    – Sunil
    Commented Sep 25, 2013 at 3:27
  • Yes, thats a useful article. Commented Sep 25, 2013 at 5:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.