Unsupported Scripts and Global functions and variables in 2016

Zoe Sands, 22 February 2016

CRM has several global functions and variables that can be used in your JavaScript's to gain information about the context that CRM is currently being used in.

Pre 2016 you accessed these functions by simply calling them directly…However in 2016 due to the use of Turbo Forms and the way that they load form content in a wrapper class (explained here) accessing the content of the IFrame is no longer direct you must first call on the parent.

e.g. The code below would have worked in the previous version.

if (IsOnline()){ alert("You are online"); }

It now needs to be changed to:

if (parent.IsOnline()){ alert("You are online"); }

NOTE: in this example I have used the IsOnline global method however wherever possible you should use the Xrm.Page methods. In this case context.client.getClientState()

This also applies to unsupported code where you are accessing the DOM.

document.getElementById("mag_fieldname").style.backgroundColor="#abcdef"

Becomes

parent.document.getElementById("mag_fieldname").style.backgroundColor="#abcdef"

NOTE: if you disable turbo forms these script changes will break. I recommend that you put in a null check and act accordingly.

var fieldElement = document.getElementById("mag_fieldname"); if (fieldElement!= null){ document.getElementById("mag_fieldname").style.backgroundColor="#abcdef" } else { parent.document.getElementById("mag_fieldname").style.backgroundColor="#abcdef" }