Sample: Detect When an Object is Available Using Script in a Web Resource and an Updated Form

[Applies to: Microsoft Dynamics CRM 2011]

This sample code is for Microsoft Dynamics CRM 2011, and can be found in the following location in the SDK download: SDK\SampleCode\JS\WebResources\Lead_Form_Test_for_Script_in_Updated_Form.htm


The HTML page represents a web resource added to the Lead entity Lead main form for a Microsoft Dynamics CRM Online organization that has Microsoft Dynamics CRM December 2012 Service Update product updates applied.


If your organization has been updated to Microsoft Dynamics CRM Online Fall ’13, please use the latest version of the SDK. Download the updated SDK package for Microsoft Dynamics CRM 2013 and CRM Online Fall ‘13.


This page shows how the existence of Xrm.Page objects should be tested before accessing them in script from web resources displayed in an updated form.


This page checks for the existence of the"subject").getValue function before calling it. If it is not ready, setTimeout is used to set a delay and check again.

<!DOCTYPE html>
    <title>Lead Form Test for Script in Updated Form</title>
 <script type="text/javascript">
  var now = null;
  var attempts = 0;
  document.onreadystatechange = function () {
   if (document.readyState == "complete") {
    now = new Date();
    writeMessage("document ready at " + now.getTime());

  function isFunctionReady() {
   var ready = false;
   try {
    ready = (typeof"subject").getValue == "function");
   catch (e) { 
   //error expected
   return ready;

  function check() {
   if (attempts > 20) {
    writeMessage("20 unsuccessfull attempts. Cancelling script");
   if (isFunctionReady()) {
   else {
    now = new Date();
    writeMessage("\"subject\").getValue not yet available at: " + now.getTime());
   setTimeout(check, 50);

  function start() {
   now = new Date();
   writeMessage("\"subject\").getValue function is available at: " + now.getTime());
   try {
    var topic ="subject").getValue();
   catch (e) {
    writeMessage("Failed to capture Topic value. Error: "+e.message);
   writeMessage("Lead Topic value: " + topic);  

  //Helper function to write data to this page:
  function writeMessage(message)
   var ol = document.getElementById("results");
   var li = document.createElement("li");
   if (typeof li.innerText != "undefined") {
   //For IE8 or lower
    li.innerText = message;
   else {
    li.textContent = message;

<ol id="results"></ol>

This web resource will display the following when added to the Lead entity Lead form and viewed using the updated presentation:

  1. document ready at 1352764181497

  2."subject").getValue not yet available at: 1352764181497

  3."subject").getValue not yet available at: 1352764181677

  4."subject").getValue not yet available at: 1352764182312

  5."subject").getValue function is available at: 1352764182879

  6. Lead Topic value: Follow-up with information regarding our promotions (sample)

When viewed using the edit mode, the object is immediately available and the web resource will display the following:

  1. document ready at 1352764345334

  2."subject").getValue function is available at: 1352764345335

  3. Lead Topic value: Follow-up with information regarding our promotions (sample)

See Also


Xrm.Page Reference


Design Considerations for Different Form Presentations

Microsoft Dynamics CRM 2011
Send comments about this topic to Microsoft.
© 2013 Microsoft Corporation. All rights reserved.