donderdag 22 mei 2008

Hide lookup fields on a crmForm

Hiding a field and caption can normally be done by setting the display property of the control to none.

Example:
crmForm.all.new_customfield.style.display = "none";
crmForm.all.new_customfield_c.style.display = "none";

The _c extensions specifies the caption of the field.
In case of a lookupfield the above code won't work bacause the "new_customfield" points to the lookup button and not the textbox.

Another way to hide fields and captions it to hide the TD tag that is wrapped arround it. This will work for all types of fields.

Example:
crmForm.all.new_customfield_d.style.display = "none";
crmForm.all.new_customfield_c.style.display = "none";

_d meaning the data field (the textbox) and _c meaning caption.

Enjoy!!

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); }