Export (0) Print
Expand All

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

Requirements

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.

noteNote
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.

Demonstrates

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.

Example

This page checks for the existence of the parent.Xrm.Page.data.entity.attributes.get("subject").getValue function before calling it. If it is not ready, setTimeout is used to set a delay and check again.

<!DOCTYPE html>
<html>
<head>
    <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());
    check();
   }
  }

  function isFunctionReady() {
   var ready = false;
   try {
    ready = (typeof parent.Xrm.Page.data.entity.attributes.get("subject").getValue == "function");
   }
   catch (e) { 
   //error expected
   }
   return ready;
  }

  function check() {
   if (attempts > 20) {
    writeMessage("20 unsuccessfull attempts. Cancelling script");
    return;
   }
   if (isFunctionReady()) {
    start();
   }
   else {
    now = new Date();
    writeMessage("parent.Xrm.Page.data.entity.attributes.get(\"subject\").getValue not yet available at: " + now.getTime());
    attempts++;
   setTimeout(check, 50);
   }
  }

  function start() {
   now = new Date();
   writeMessage("parent.Xrm.Page.data.entity.attributes.get(\"subject\").getValue function is available at: " + now.getTime());
   try {
    var topic = parent.Xrm.Page.data.entity.attributes.get("subject").getValue();
   }
   catch (e) {
    writeMessage("Failed to capture Topic value. Error: "+e.message);
   }
   //success
   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.appendChild(li);
  }

 </script>
</head>
<body>
<ol id="results"></ol>
</body>
</html>

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. parent.Xrm.Page.data.entity.attributes.get("subject").getValue not yet available at: 1352764181497

  3. parent.Xrm.Page.data.entity.attributes.get("subject").getValue not yet available at: 1352764181677

  4. parent.Xrm.Page.data.entity.attributes.get("subject").getValue not yet available at: 1352764182312

  5. parent.Xrm.Page.data.entity.attributes.get("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. parent.Xrm.Page.data.entity.attributes.get("subject").getValue function is available at: 1352764345335

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

See Also

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

Community Additions

Show:
© 2014 Microsoft