Tuesday 31 May 2011

'Xrm.Page.ui' is null or not an object

It may be more of a silly mistake on my side, but I ran into an interesting problem today that took me a while to resolve. Since I cannot find any information on this anywhere on the web, I thought I may as well write a blog entry on it.

While trying to access the "Xrm.Page.ui" object, I kept on getting an error saying:
'Xrm.Page.ui' is null or not an object.
Turns out that somewhere along the line, I did not have my code inside my JScript Resource file wrapped in a method, and I did not actually call this method on load of the form.

So I simply had:

alert(Xrm.Page.ui.getFormType());

Instead of:
function myFunction()
{
alert(Xrm.Page.ui.getFormType());
}

where the above function is then called on load of the entity.

This lead to a few interesting observations:
  1. It is possible to execute code BEFORE onLoad, by simply not wrapping the code in the Web Resource in a function. Even though this is true, I'd say it is probably very bad practice to "forget" to wrap it in a function/
  2. When JScript is not wrapped in a function, it not only executes BEFORE onLoad, it executes before any Crm objects are initialized. So the developer does not have access to any CRM SDK objects at this point.
  3. I should STOP making noob mistakes ;)

Paul Reyneke

1 comment:

  1. And 5 years later someone else makes the same mistake :)

    ReplyDelete