Executing client side script when an associated entity is added


It is sometimes required to capture the event when an associated entity is added. This event cannot be captured in a “supported way”.

To do so we first have to load the associated view to make sure the object is available. After it is loaded we can retrieve the crmGrid object that is located inside the IFRAME. Attach a function to the onrefresh event of this control and you can start doing you coding.
Call a webservice to do some async calls or just update some fields on the CRM form.

Add this code to the OnLoad of the CRM form. Make sure you also replace the control names with your own controls. (You can easily find them using the IE Developer Toolbar)

//========================================================================= 
// Author: Kenny Vaes 
// Creation date: 22/05/2008 
// Description: Execute client side code when an associated entity is added 
//========================================================================= 

try
{
var script = document.createElement("script");
script.language = "javascript";
document.getElementsByTagName('head')[0].appendChild(script);

// Create a function that will execute when the gridview of the associated entity refreshes 
var GridRefresh = function()
{
 var AssocGridBody = document.getElementById("areametris_incident_assetFrame").contentWindow.document.getElementById("gridBodyTable");
 if (AssocGridBody)
 {
  var CurrentRowCount = AssocGridBody.getElementsByTagName('tr').length;

  // If the current rowcount = 1 it is possible that there is no data in the grid
 // In this case the grid will only contain 1 colomn
  if (CurrentRowCount == 1)
  {
     if (AssocGridBody.getElementsByTagName('tr')[0].getElementsByTagName('td').length == 1)
     {
      CurrentRowCount = 0;
   }
 }

  if (CurrentRowCount > PreviousRowCount)
  {
   PreviousRowCount = CurrentRowCount;
   // Do Stuff... 
  }
 }
}

//Create a function that executes after the form has loaded. 
var FormLoaded = function()
{
 if ((event.srcElement.readyState == "complete") (event.srcElement.readyState == "loaded")) {
  var menubuttonassets = document.getElementById("navmetris_incident_asset");
  if (menubuttonassets)
  {
   // Perform a click to open the associated view in the iframe 
   menubuttonassets.click();

   // Set the focus back to the area screen. 
   loadArea('areaForm');

  }

  var assocView = document.getElementById("areametris_incident_assetFrame");
  if (assocView)
  {
   // Add an event to the readystate change of the iframe 
   assocView.onreadystatechange = function ()
   {
    if (assocView.readyState == "complete")
    {
     // Get the gridview inside the iframe and attach an event. 
     var crmGridAssets = assocView.contentWindow.document.getElementById("crmGrid");
     if (crmGridAssets)
     {
      // Attach a function to the onrefresh of the grid 
      crmGridAssets.attachEvent("onrefresh", GridRefresh);
      var AssocGridBody = document.getElementById("areametris_incident_assetFrame").contentWindow.document.getElementById("gridBodyTable");
      if (AssocGridBody)
      {
       PreviousRowCount = AssocGridBody.getElementsByTagName('tr').length;
       if (PreviousRowCount == 1) PreviousRowCount = 0;
      }
     }
    }
   }
  }
 }
}
script.attachEvent("onreadystatechange", FormLoaded);

} catch (e) { alert(e); }
Advertisements
Tagged with: ,
Posted in CRM 4
One comment on “Executing client side script when an associated entity is added
  1. Rachele says:

    It’s nearly impossible to find well-informed people about tɦis topic, Ƅut you sound likе you know what ƴou’re
    talking about! Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: